Skip to content

Commit f397026

Browse files
Kotlin extensions: ToMany applyChangesToDb
1 parent adcc8be commit f397026

File tree

1 file changed

+19
-0
lines changed
  • objectbox-kotlin/src/main/kotlin/io/objectbox/kotlin

1 file changed

+19
-0
lines changed

objectbox-kotlin/src/main/kotlin/io/objectbox/kotlin/Extensions.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
* limitations under the License.
1515
*/
1616

17+
@file:Suppress("unused") // tested in integration test project
18+
1719
package io.objectbox.kotlin
1820

1921
import io.objectbox.Box
2022
import io.objectbox.BoxStore
2123
import io.objectbox.Property
2224
import io.objectbox.query.Query
2325
import io.objectbox.query.QueryBuilder
26+
import io.objectbox.relation.ToMany
2427
import kotlin.reflect.KClass
2528

2629
inline fun <reified T> BoxStore.boxFor(): Box<T> = boxFor(T::class.java)
@@ -49,3 +52,19 @@ inline fun <T> Box<T>.query(block: QueryBuilder<T>.() -> Unit) : Query<T> {
4952
block(builder)
5053
return builder.build()
5154
}
55+
56+
/**
57+
* Allows making changes (adding and removing entities) to this ToMany with a call to
58+
* [apply][ToMany.applyChangesToDb] the changes to the database.
59+
* Can [reset][ToMany.reset] the ToMany before making changes.
60+
* ```
61+
* toMany.applyChangesToDb {
62+
* add(entity)
63+
* }
64+
* ```
65+
*/
66+
inline fun <T> ToMany<T>.applyChangesToDb(resetFirst: Boolean = false, body: ToMany<T>.() -> Unit) {
67+
if (resetFirst) reset()
68+
body()
69+
applyChangesToDb()
70+
}

0 commit comments

Comments
 (0)