use of io.reactivex.FlowableEmitter in project kripton by xcesco.
the class BindXenoDataSource method execute.
public <T> Flowable<T> execute(final FlowableTransaction<T> transaction) {
FlowableOnSubscribe<T> emitter = new FlowableOnSubscribe<T>() {
@Override
public void subscribe(FlowableEmitter<T> emitter) {
boolean needToOpened = !BindXenoDataSource.this.isOpenInWriteMode();
boolean success = false;
@SuppressWarnings("resource") SQLiteDatabase connection = needToOpened ? openWritableDatabase() : database();
DataSourceSingleThread currentDaoFactory = _daoFactorySingleThread.bindToThread();
currentDaoFactory.onSessionOpened();
try {
connection.beginTransaction();
if (transaction != null && TransactionResult.COMMIT == transaction.onExecute(currentDaoFactory, emitter)) {
connection.setTransactionSuccessful();
success = true;
}
emitter.onComplete();
} catch (Throwable e) {
Logger.error(e.getMessage());
e.printStackTrace();
emitter.onError(e);
currentDaoFactory.onSessionClear();
} finally {
try {
connection.endTransaction();
} catch (Throwable e) {
}
if (needToOpened) {
close();
}
if (success) {
currentDaoFactory.onSessionClosed();
} else {
currentDaoFactory.onSessionClear();
}
}
return;
}
};
Flowable<T> result = Flowable.create(emitter, BackpressureStrategy.BUFFER);
if (globalSubscribeOn != null)
result.subscribeOn(globalSubscribeOn);
if (globalObserveOn != null)
result.observeOn(globalObserveOn);
return result;
}
Aggregations