Skip to content
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 · 3 comments
Open

Additional Kotlin extensions #446

greenrobot-team opened this issue May 14, 2018 · 3 comments
Assignees
Labels

Comments

@greenrobot-team
Copy link
Member

@greenrobot-team 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
Copy link
Member Author

@greenrobot-team 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
Copy link
Member Author

@greenrobot-team 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

@greenrobot-team
Copy link
Member Author

@greenrobot-team greenrobot-team commented Jan 22, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant