Skip to content

Instantly share code, notes, and snippets.

@robertlevonyan
Created March 4, 2021 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertlevonyan/4cb099dd28391a2658356fafb537fdbf to your computer and use it in GitHub Desktop.
Save robertlevonyan/4cb099dd28391a2658356fafb537fdbf to your computer and use it in GitHub Desktop.
class ObjectBoxDao() : DbRepository {
// create a Box instance for Movie type
private val moviesBox: Box<Movie> = ObjectBox.boxStore.boxFor(ObMovie::class.java)
override suspend fun save(movies: List<Movie>) {
moviesBox.put(movies)
}
override fun getMovies(): Flow<List<Movie>> = callbackFlow { // wrap response in a callback flow
val subscription = moviesBox.query().build().subscribe()
.observer { data -> offer(data.map { it.toMovie() }) }
awaitClose { subscription.cancel() }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment