use Rx to insert conversations and messages into realm asyncronously

This commit is contained in:
Moez Bhatti
2017-08-14 22:46:02 -04:00
parent 26a7fe72c8
commit 52d2116d10
10 changed files with 95 additions and 40 deletions
@@ -1,6 +1,7 @@
package com.moez.QKSMS.util
import android.database.Cursor
import io.reactivex.Flowable
fun Cursor.forEach(closeOnComplete: Boolean = true, method: (Cursor) -> Unit = {}) {
moveToPosition(-1)
@@ -11,4 +12,21 @@ fun Cursor.forEach(closeOnComplete: Boolean = true, method: (Cursor) -> Unit = {
if (closeOnComplete) {
close()
}
}
}
/**
* We're using this simple implementation with .range() because of the
* complexities of dealing with Backpressure with a Cursor. We can't simply
* use a loop and call onNext() from a generator because we'll need to close
* the cursor at the end, and if any items are still in the buffer, then
* they will be made invalid
*/
fun Cursor.asFlowable(): Flowable<Cursor> {
return Flowable.range(0, count)
.map {
moveToPosition(it)
this
}
.doOnComplete { close() }
}