Insert 1 conversation into realm at a time

This commit is contained in:
Moez Bhatti
2017-09-19 18:49:48 -04:00
parent 25bca8dadd
commit bb676e499e
@@ -26,13 +26,19 @@ class SyncManager(val context: Context, val contacts: ContactRepository) {
.doOnNext { .doOnNext {
// We need to set up realm on the io thread, and doOnSubscribe doesn't support setting a custom Scheduler // We need to set up realm on the io thread, and doOnSubscribe doesn't support setting a custom Scheduler
realm = Realm.getDefaultInstance() realm = Realm.getDefaultInstance()
realm?.beginTransaction() realm?.executeTransaction {
realm?.deleteAll() it.delete(Conversation::class.java)
it.delete(Message::class.java)
}
} }
.flatMap { cursor -> cursor.asFlowable() } .flatMap { cursor -> cursor.asFlowable() }
.map { cursor -> Conversation(cursor, contacts) } .map { cursor -> Conversation(cursor, contacts) }
.distinct { conversation -> conversation.id } .distinct { conversation -> conversation.id }
.doOnNext { conversation -> realm?.insertOrUpdate(conversation) } .doOnNext { conversation ->
realm?.beginTransaction()
realm?.insertOrUpdate(conversation)
}
.doAfterNext { realm?.commitTransaction() }
.map { conversation -> conversation.id } .map { conversation -> conversation.id }
.concatMap { threadId -> .concatMap { threadId ->
val uri = Uri.withAppendedPath(MessageColumns.URI, threadId.toString()) val uri = Uri.withAppendedPath(MessageColumns.URI, threadId.toString())
@@ -45,14 +51,9 @@ class SyncManager(val context: Context, val contacts: ContactRepository) {
.doOnNext { message -> realm?.insertOrUpdate(message) } .doOnNext { message -> realm?.insertOrUpdate(message) }
.count() .count()
.toFlowable() .toFlowable()
.doOnNext { .doOnNext { realm?.close() }
realm?.commitTransaction()
realm?.close()
}
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe({ .subscribe { completionListener.invoke() }
completionListener.invoke()
})
} }
} }