GeezerVison is showing github.com — Add BoxStore.awaitCallInTx suspend function. · hehuelet/objectbox-java@82451da
File tree
Expand file treeCollapse file tree
Open diff view settings
objectbox-kotlin/src/main/kotlin/io/objectbox/kotlin tests/objectbox-java-test src/test/java/io/objectbox
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: objectbox-kotlin/src/main/kotlin/io/objectbox/kotlin/BoxStore.kt +24Lines changed: 24 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,6 +18,10 @@ package io.objectbox.kotlin |
18 | 18 |
|
19 | 19 | import io.objectbox.Box |
20 | 20 | import io.objectbox.BoxStore |
| 21 | +import java.util.concurrent.Callable |
| 22 | +import kotlin.coroutines.resume |
| 23 | +import kotlin.coroutines.resumeWithException |
| 24 | +import kotlin.coroutines.suspendCoroutine |
21 | 25 | import kotlin.reflect.KClass |
22 | 26 |
|
23 | 27 |
|
@@ -27,3 +31,23 @@ inline fun <reified T> BoxStore.boxFor(): Box<T> = boxFor(T::class.java) |
27 | 31 | /** Shortcut for `BoxStore.boxFor(Entity::class.java)`. */ |
28 | 32 | @Suppress("NOTHING_TO_INLINE") |
29 | 33 | inline fun <T : Any> BoxStore.boxFor(clazz: KClass<T>): Box<T> = boxFor(clazz.java) |
| 34 | + |
| 35 | +/** |
| 36 | + * Wraps [BoxStore.callInTxAsync] in a coroutine that suspends until the transaction has completed. |
| 37 | + * Likewise, on success the return value of the given [callable] is returned, on failure an exception is thrown. |
| 38 | + * |
| 39 | + * Note that even if the coroutine is cancelled the callable is always executed. |
| 40 | + * |
| 41 | + * The callable (and transaction) is submitted to the ObjectBox internal thread pool. |
| 42 | + */ |
| 43 | +suspend fun <V : Any> BoxStore.awaitCallInTx(callable: Callable<V?>): V? { |
| 44 | + return suspendCoroutine { continuation -> |
| 45 | + callInTxAsync(callable) { result, error -> |
| 46 | + if (error != null) { |
| 47 | + continuation.resumeWithException(error) |
| 48 | + } else { |
| 49 | + continuation.resume(result) |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | +} |
Collapse file
Copy file name to clipboardExpand all lines: tests/objectbox-java-test/build.gradle +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -54,6 +54,8 @@ dependencies { |
54 | 54 | } |
55 | 55 |
|
56 | 56 | testImplementation "junit:junit:$junit_version" |
| 57 | + // To test Coroutines |
| 58 | + testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version") |
57 | 59 | // To test Kotlin Flow |
58 | 60 | testImplementation 'app.cash.turbine:turbine:0.5.2' |
59 | 61 | } |
|
Collapse file
Copy file name to clipboardExpand all lines: tests/objectbox-java-test/src/test/java/io/objectbox/BoxStoreTestK.kt +23-1Lines changed: 23 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
|
1 | 1 | package io.objectbox |
2 | 2 |
|
| 3 | +import io.objectbox.kotlin.awaitCallInTx |
3 | 4 | import io.objectbox.kotlin.boxFor |
| 5 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 6 | +import kotlinx.coroutines.test.runTest |
4 | 7 | import org.junit.Assert.assertEquals |
5 | 8 | import org.junit.Test |
6 | 9 |
|
7 | 10 |
|
8 | | -class BoxStoreTestK: AbstractObjectBoxTest() { |
| 11 | +class BoxStoreTestK : AbstractObjectBoxTest() { |
9 | 12 |
|
10 | 13 | /** |
11 | 14 | * This is mostly to test the expected syntax works without errors or warnings. |
@@ -20,4 +23,23 @@ class BoxStoreTestK: AbstractObjectBoxTest() { |
20 | 23 | val box2 = store.boxFor(TestEntity::class) |
21 | 24 | assertEquals(boxJavaApi, box2) |
22 | 25 | } |
| 26 | + |
| 27 | + @ExperimentalCoroutinesApi |
| 28 | + @Test |
| 29 | + fun awaitCallInTx() { |
| 30 | + val box = store.boxFor<TestEntity>() |
| 31 | + runTest { |
| 32 | + // put |
| 33 | + val id = store.awaitCallInTx { |
| 34 | + box.put(createTestEntity("Hello", 1)) |
| 35 | + } |
| 36 | + assertEquals(1, id!!) |
| 37 | + |
| 38 | + // get |
| 39 | + val note = store.awaitCallInTx { |
| 40 | + box.get(id) |
| 41 | + } |
| 42 | + assertEquals("Hello", note!!.simpleString) |
| 43 | + } |
| 44 | + } |
23 | 45 | } |