[Question] Any way to store data received from Observable in one transaction?
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?
So far, transactions require a Runnable or Callable. Could you post the full code example? Maybe there's a workaround...
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 This solution is even superior because it minimizes the time of the write transaction being active.