kotlinobjectbox

ObjectBox-Java - Most performant way to do massive updates


I'm using objectbox-java on my Android app. One of the features I'm implementing requires to switch on and off a flag on certain ObjectBox entities, which can reach the 100.000+ count for some users.

I'd like to know if the code I'm using to do these updates can be optimized, performance-wise.

fun flagEntitiesForDeletion(ids: LongArray, flagValue: Boolean) {
  val store = store.boxFor(MyEntity::class.java)
  val entities = store.get(ids)
  entities.forEach { it.isFlaggedForDeletion = flagValue }
  store.put(entities)
}

For instance, would using an overarching transaction for get and put perform better in that case?

Any suggestion is welcome~


Solution

  • Performance wise, it looks good: there's one read transaction and one write transaction implicitly created. Both doing bulk operation, so it's fast.

    Just a few comments: it would be transactionally safer to use one transaction as there could be competing changes (depends on your app); e.g. do everything in one (write) transaction. (As you suggested in an overarching transaction.)

    Regarding memory consumption: for getting a huge number (100k) of object you will need a bit of memory. It might be fine, but an alternative would be iterate over the IDs (get one and put one). Saves memory, and it would put less pressure on the garbage collector. There are trade-offs either way, so testing both variations would be interesting to test to see which version is faster.