Transactions in Android Room w/ RxJava2
By logging the current thread and perusing Android developer documentation I think I might finally understand what I'm doing wrong.
1) Transactions must occur on the same thread. That's why it's telling me there's no transaction; I'm apparently bouncing between threads.
2) The doOnSubscribe, doOnComplete, and doFinally methods are side effects and therefore not part of the actual stream itself. That means they will not occur on the scheduler I subscribe on. They will occur on the Scheduler I observe on.
3) Because I want to receive results on the UI thread upon completion, but want the side effects to occur on a background thread, I need to change the position in which I observe on.
Completable.concatArray(one, two, three, four)
.observeOn(Schedulers.single()) // OFF UI THREAD
.doOnSubscribe(__ -> {
Log.w(LOG_TAG, "Begin transaction. " + Thread.currentThread().toString());
mRoomDatabase.beginTransaction();
})
.doOnComplete(() -> {
Log.w(LOG_TAG, "Set transaction successful." + Thread.currentThread().toString());
mRoomDatabase.setTransactionSuccessful();
})
.doFinally(() -> {
Log.w(LOG_TAG, "End transaction." + Thread.currentThread().toString());
mRoomDatabase.endTransaction();
})
.subscribeOn(Schedulers.single())
.observeOn(AndroidSchedulers.mainThread()) // ON UI THREAD
.subscribeWith(new CompletableObserver() {
@Override
public void onSubscribe(Disposable d) {
Log.w(LOG_TAG, "onSubscribe." + Thread.currentThread().toString());
}
@Override
public void onComplete() {
Log.w(LOG_TAG, "onComplete." + Thread.currentThread().toString());
}
@Override
public void onError(Throwable e) {
Log.e(LOG_TAG, "onError." + Thread.currentThread().toString());
}
});
The logging statements now look like this:
W/MyPresenter: onSubscribe.Thread[main,5,main]
W/MyPresenter: Begin transaction. Thread[RxSingleScheduler-1,5,main]
W/MyPresenter: (1)
W/MyPresenter: (2)
W/MyPresenter: (3)
W/MyPresenter: (4)
W/MyPresenter: Set transaction successful.Thread[RxSingleScheduler-1,5,main]
W/MyPresenter: End transaction.Thread[RxSingleScheduler-1,5,main]
W/MyPresenter: onComplete.Thread[main,5,main]
I believe this accomplishes what I'm after but it remains to be seen if the step-by-step assembly of Room-based RxJava Completables will work out. I will keep an eye out for any comments/answers and may report back for posterity.