Update only selected columns in ObjectBox Android

581 views Asked by At

Is it possible that update only selected columns in ObjectBox Android?

For example, there are 5 columns in "Remind" Entity

@Entity
data class Remind (

    @Id var id: Long = 0,
    var title: String? = null, 
    var memo: String? = null,
    var ymd: String? = "2021-01-01 FRI"
    var done: Int = 0
)

and I want to update only "done" column for existing ID.

So I tried,

private fun putRemind(done: Int, id: Long) {
        val newRemind = Remind(done = done, id = id)
        ObjectBox.store.boxFor(Remind::class.java).put(newRemind)
    }

however, all columns were overwritten (other columns were filled with initial values).

Is there any easy way to overwrite only selected columns? Or always do I have to re-put all the columns?

Thank you.

1

There are 1 answers

1
Markus Junginger On BEST ANSWER

First, there's no notion of a column in ObjectBox, e.g. it's not a relational database. It stores objects and it does so extremely fast. Thus, the way to update is to get an object (if you do not already have it), update one or as many properties you want and then put(). Another upside of this is that you don't need an update method for each individual property.