objectbox-java icon indicating copy to clipboard operation
objectbox-java copied to clipboard

Additional Kotlin extensions

Open greenrobot-team opened this issue 7 years ago • 9 comments

ObjectBox already provides some minimal Kotlin extensions:

// Regular
val box: Box<DataClassEntity> = store.boxFor(DataClassEntity::class.java)

// With extension
val box: Box<DataClassEntity> = store.boxFor()
// Regular
val query = box.query().`in`(property, array).build()

// With extension
val query = box.query().inValues(property, array).build()

This issue is to track what other Kotlin extensions may be added.

Some ground rules:

  • should leverage features unique to Kotlin
  • should not just be a shorter version of an existing method
  • should not optimize fur just a single use case

Things to look out for:

  • default args for methods
  • builder pattern
  • making util methods discoverable from the type they operate on
  • destructuring

-ut

greenrobot-team avatar May 14 '18 13:05 greenrobot-team

My suggestions:

  • shipped with 2.0.0 beta Box query builder:
// Regular
val query = box.query().run {
    equal(property, value)
    order(property)
    build()
}

// With extension
val query = box.query {
    equal(property, value)
    order(property)
}
  • shipped with 2.0.0 beta ToMany applyChangesToDb
// Regular
toMany.apply { 
    reset()
    add(entity)
    removeById(id)
    applyChangesToDb()
}

// With extension
toMany.applyChangesToDb(resetFirst = true) { // default is false
    add(entity)
    removeById(id)
}
  • Discover ObjectBoxLiveData from Query autocomplete (note: this only works with objectbox-android, so add objectbox-android-ktx? ~~Or compileOnly objectbox-android?~~)
// Regular
val liveData = ObjectBoxLiveData(query)

// With extension
val liveData = query.toLiveData()

-ut

greenrobot-team avatar May 14 '18 13:05 greenrobot-team

The first two suggestions are released as part of 2.0.0-beta. Also added docs for the existing extensions. -ut

greenrobot-team avatar Jul 03 '18 09:07 greenrobot-team

An upcoming release will add extensions that are shortcuts for query methods that accept Long and Double. However, the shortcuts can be used with Int, Short and Float without having to call .toLong() or .toDouble():

// Regular
queryBuilder.equal(Model_.type, typeId.toLong())

// With extension
queryBuilder.equal(Model_.type, typeId)

-ut

greenrobot-team avatar Jan 22 '19 09:01 greenrobot-team

Could we also get "notIn" added for String Arrays or something the equivalent of not inValues for the Strings?

irvine752 avatar Dec 30 '21 08:12 irvine752

@irvine752 This issue is about Kotlin extension functions, not query conditions. Anyhow, not-in is not supported for String properties. Please thumbs up #941!

greenrobot-team avatar Jan 10 '22 07:01 greenrobot-team

What about :

val query = box.query().notIn(User_.phone, stringListPhoneNumbers)

notIn usage Error:

None of the following functions can be called with the arguments supplied.

  • notIn(Property<User!>!, IntArray!) defined in io.objectbox.query.QueryBuilder

  • notIn(Property<User!>!, LongArray!) defined in io.objectbox.query.QueryBuilder

HosseinKurd avatar Feb 08 '22 12:02 HosseinKurd

@HosseinKurd The notIn (or notOneOf using the new query API) conditions are only supported for integers (Integer and Long)! For Strings only in/oneOf is possible.

greenrobot-team avatar Feb 14 '22 07:02 greenrobot-team

@HosseinKurd The notIn (or notOneOf using the new query API) conditions are only supported for integers (Integer and Long)! For Strings only in/oneOf is possible.

So how can I Reverse in/oneOf? there is not any not() function

HosseinKurd avatar Feb 14 '22 07:02 HosseinKurd

@HosseinKurd You can't. Put differently: notIn is not supported for String. You'll have to find another way, e.g. by changing your model or writing custom filter code.

greenrobot-team avatar Feb 14 '22 14:02 greenrobot-team