Skip to content

Commit e69b469

Browse files
committed
Merge remote-tracking branch 'origin/more-ktx' into dev
# Conflicts: # build.gradle
2 parents fb61131 + f397026 commit e69b469

File tree

1 file changed

+36
-2
lines changed
  • objectbox-kotlin/src/main/kotlin/io/objectbox/kotlin

1 file changed

+36
-2
lines changed

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +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
24+
import io.objectbox.query.Query
2225
import io.objectbox.query.QueryBuilder
26+
import io.objectbox.relation.ToMany
2327
import kotlin.reflect.KClass
2428

2529
inline fun <reified T> BoxStore.boxFor(): Box<T> = boxFor(T::class.java)
@@ -28,9 +32,39 @@ inline fun <reified T> BoxStore.boxFor(): Box<T> = boxFor(T::class.java)
2832
inline fun <T : Any> BoxStore.boxFor(clazz: KClass<T>): Box<T> = boxFor(clazz.java)
2933

3034
/** An alias for the "in" method, which is a reserved keyword in Kotlin. */
31-
inline fun <reified T> QueryBuilder<T>.inValues(property: Property, values: LongArray): QueryBuilder<T>?
35+
inline fun <reified T> QueryBuilder<T>.inValues(property: Property, values: LongArray): QueryBuilder<T>
3236
= `in`(property, values)
3337

3438
/** An alias for the "in" method, which is a reserved keyword in Kotlin. */
35-
inline fun <reified T> QueryBuilder<T>.inValues(property: Property, values: IntArray): QueryBuilder<T>?
39+
inline fun <reified T> QueryBuilder<T>.inValues(property: Property, values: IntArray): QueryBuilder<T>
3640
= `in`(property, values)
41+
42+
/**
43+
* Allows building a query for this Box instance with a call to [build][QueryBuilder.build] to return a [Query] instance.
44+
* ```
45+
* val query = box.query {
46+
* equal(Entity_.property, value)
47+
* }
48+
* ```
49+
*/
50+
inline fun <T> Box<T>.query(block: QueryBuilder<T>.() -> Unit) : Query<T> {
51+
val builder = query()
52+
block(builder)
53+
return builder.build()
54+
}
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)