Skip to content
This repository has been archived by the owner on Apr 25, 2021. It is now read-only.

Commit

Permalink
fix: Add 'isActive' check for suspended couroutine
Browse files Browse the repository at this point in the history
This commit adds a call to the `isActive` method to prevent resuming/cancelling of the coroutine after it has been completed

Other changes:
- Bump up compile sdk 28 -> 29
  • Loading branch information
michaelbukachi committed Aug 13, 2019
1 parent 87020f7 commit c94107d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ task clean(type: Delete) {

ext {
minSdkVersion = 16
compileSdkVersion = 28
compileSdkVersion = 29
coroutinesVersion = "1.3.0-M1"

bintrayRepo = 'realm'
Expand All @@ -51,7 +51,7 @@ ext {
siteUrl = 'https://github.com/michaelbukachi/realm-koroutines'
gitUrl = 'https://github.com/michaelbukachi/realm-koroutines.git'

libraryVersion = '0.1.1'
libraryVersion = '0.1.2'

developerId = 'michaelbukachi'
developerName = 'Michael Bukachi'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,46 @@ import kotlin.coroutines.resumeWithException

private suspend fun <T : RealmObject, S : RealmQuery<T>> findAllAwait(query: S): RealmResults<T> =
suspendCancellableCoroutine { continuation ->
val listener = RealmChangeListener<RealmResults<T>> { t -> continuation.resume(t) }
val listener = RealmChangeListener<RealmResults<T>> { t ->
if (continuation.isActive) {
continuation.resume(t)
}
}
query.findAllAsync().addChangeListener(listener)
}

private suspend fun <T : RealmObject, S : RealmQuery<T>> findFirstAwait(query: S): T? =
suspendCancellableCoroutine { continuation ->
val listener = RealmChangeListener { t: T? -> continuation.resume(t) }
val listener = RealmChangeListener { t: T? ->
if (continuation.isActive) {
continuation.resume(t)
}
}
query.findFirstAsync().addChangeListener(listener)
}

private suspend fun <T : RealmObject, S : RealmQuery<T>> findAllAwaitOffline(query: S): List<T> =
suspendCancellableCoroutine { continuation ->
val realm = query.realm
val listener = RealmChangeListener<RealmResults<T>> { t -> continuation.resume(realm.copyFromRealm(t)) }
val listener = RealmChangeListener<RealmResults<T>> { t ->
if (continuation.isActive) {
continuation.resume(realm.copyFromRealm(t))
}
}
query.findAllAsync().addChangeListener(listener)
}

private suspend fun <T : RealmObject, S : RealmQuery<T>> findFirstAwaitOffline(query: S): T? =
suspendCancellableCoroutine { continuation ->
val realm = query.realm
val listener = RealmChangeListener { t: T? ->
t?.let {
continuation.resume(realm.copyFromRealm(it))
} ?: run {
continuation.resume(t)
if (continuation.isActive) {
t?.let {
continuation.resume(realm.copyFromRealm(it))
} ?: run {
continuation.resume(t)
}
}

}
query.findFirstAsync().addChangeListener(listener)
}
Expand Down
1 change: 1 addition & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ dependencies {
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutinesVersion"
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.michaelbukachi.realmkoroutines.sample

import androidx.test.annotation.UiThreadTest
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.michaelbukachi.realmkoroutines.allOffline
import com.michaelbukachi.realmkoroutines.awaitAllOffline
import com.michaelbukachi.realmkoroutines.awaitFirstOffline
import com.michaelbukachi.realmkoroutines.firstOffline
import io.realm.RealmResults
import io.realm.kotlin.where
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
import org.junit.After
import org.junit.Assert
import org.junit.Before
Expand Down Expand Up @@ -47,16 +48,19 @@ class RealmKtTest {
Assert.assertFalse(resultList is RealmResults)
}

@UiThreadTest
@Test
fun testAsyncOffline() = runBlocking(Dispatchers.Main) {
val realm = testRealm()
val result = realm.where<TestObject>().awaitFirstOffline()
Assert.assertEquals("Some Test", result!!.name)
fun testAsyncOffline(): Unit = runBlocking {
withTimeout(10000) {
val realm = testRealm()
val result = realm.where<TestObject>().awaitFirstOffline()
Assert.assertEquals("Some Test", result!!.name)

val resultList = realm.where<TestObject>().awaitAllOffline()
Assert.assertTrue(resultList.isNotEmpty())
Assert.assertFalse(resultList is RealmResults)
}
val resultList = realm.where<TestObject>().awaitAllOffline()
Assert.assertTrue(resultList.isNotEmpty())
Assert.assertFalse(resultList is RealmResults)
}

}

}

0 comments on commit c94107d

Please sign in to comment.