New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional Kotlin extensions #446

Open
greenrobot-team opened this Issue May 14, 2018 · 2 comments

Comments

Projects
None yet
1 participant
@greenrobot-team
Collaborator

greenrobot-team commented May 14, 2018

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

This comment has been minimized.

Collaborator

greenrobot-team commented May 14, 2018

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

This comment has been minimized.

Collaborator

greenrobot-team commented Jul 3, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment