diff --git a/qksms/src/main/java/com/moez/QKSMS/data/model/Conversation.kt b/qksms/src/main/java/com/moez/QKSMS/data/model/Conversation.kt index cd04f595..8cfc34d4 100644 --- a/qksms/src/main/java/com/moez/QKSMS/data/model/Conversation.kt +++ b/qksms/src/main/java/com/moez/QKSMS/data/model/Conversation.kt @@ -30,7 +30,7 @@ open class Conversation() : RealmObject() { Flowable.fromIterable(cursor.getString(ConversationColumns.RECIPIENT_IDS).split(" ")) .map { id -> id.toLong() } - .map { id -> contacts.getContact(id) } + .map { id -> contacts.getContactBlocking(id) } .filter { contact -> contact.recipientId != 0L } .blockingSubscribe { contact -> this.contacts.add(contact) } diff --git a/qksms/src/main/java/com/moez/QKSMS/data/repository/ContactRepository.kt b/qksms/src/main/java/com/moez/QKSMS/data/repository/ContactRepository.kt index 302f9f81..ac997595 100644 --- a/qksms/src/main/java/com/moez/QKSMS/data/repository/ContactRepository.kt +++ b/qksms/src/main/java/com/moez/QKSMS/data/repository/ContactRepository.kt @@ -21,29 +21,19 @@ class ContactRepository(val context: Context) { * This function is not asynchronous, and potentially very slow. Make sure to * execute off of the main thread. At the time of writing */ - fun getContact(recipientId: Long): Contact { - var contact: Contact? = getContactFromRealm(recipientId) - if (contact == null) { - contact = getContactFromContentProvider(recipientId) - } - - return contact ?: Contact() - } - - private fun getContactFromRealm(recipientId: Long): Contact? { + fun getContactBlocking(recipientId: Long): Contact { + // First, try loading the conversation from Realm val realm = Realm.getDefaultInstance() val results = realm.where(Contact::class.java) .equalTo("recipientId", recipientId) .findAll() if (results.size > 0) { + realm.close() return results[0] } - return null - } - - private fun getContactFromContentProvider(recipientId: Long): Contact? { + // If it's not found, then load from the Contacts ContentProvider instead var contact: Contact? = null val recipientCursor = context.contentResolver.query(ContentUris.withAppendedId(URI, recipientId), null, null, null, null) @@ -64,7 +54,13 @@ class ContactRepository(val context: Context) { contactCursor?.close() } - return contact + if (contact != null) { + realm.close() + return contact + } + + // If it's still not found then return an empty Contact object + return Contact() } } \ No newline at end of file diff --git a/qksms/src/main/java/com/moez/QKSMS/data/sync/SyncManager.kt b/qksms/src/main/java/com/moez/QKSMS/data/sync/SyncManager.kt index 9832b5eb..60a125ad 100644 --- a/qksms/src/main/java/com/moez/QKSMS/data/sync/SyncManager.kt +++ b/qksms/src/main/java/com/moez/QKSMS/data/sync/SyncManager.kt @@ -24,7 +24,7 @@ class SyncManager(val context: Context, val contacts: ContactRepository) { Flowable.just(conversationsCursor) .subscribeOn(Schedulers.io()) .doOnNext { - // We need to setup 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?.beginTransaction() realm?.deleteAll()