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

[Question] Any way to store data received from Observable in one transaction?

Open ferbeb opened this issue 9 years ago • 3 comments

I'm thinking about opening a transaction in .doOnSubscribe, persisting in .doOnNext and eventually completing/rolling back transaction in .doOnComplete and .doOnError.

Am I overlooking something or is this impossible without caching all received objects?

ferbeb avatar Oct 23 '17 11:10 ferbeb

So far, transactions require a Runnable or Callable. Could you post the full code example? Maybe there's a workaround...

greenrobot avatar Oct 23 '17 11:10 greenrobot

The workaround would look like this, I suppose:

ObservableTransformer<Data, Data> {
            val cache = mutableListOf<Data>()

            it.doOnNext { cache+= it }
            it.doOnComplete {
                val box = App.instance.boxStore.boxFor(Data::class.java)
                box.put(cache)
            }

I think this would be sufficient to make sure data is only persisted when there is no error in the Observable.

Of course, this would not work if a lot of data was used that does not fit into the cache. With Realm, I had the control to begin and commit/rollback transactions manually, allowing me to write whenever data was received and then decide on whether to commit or rollback based on onComplete/onError.

ferbeb avatar Oct 23 '17 11:10 ferbeb

@ferbeb This solution is even superior because it minimizes the time of the write transaction being active.

greenrobot avatar Oct 23 '17 12:10 greenrobot