Search in sources :

Example 6 with FlowableEmitter

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;
}
Also used : FlowableOnSubscribe(io.reactivex.FlowableOnSubscribe) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) FlowableEmitter(io.reactivex.FlowableEmitter)

Aggregations

FlowableEmitter (io.reactivex.FlowableEmitter)6 FlowableOnSubscribe (io.reactivex.FlowableOnSubscribe)6 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)4 ApplicationInfo (android.content.pm.ApplicationInfo)1 PackageManager (android.content.pm.PackageManager)1 Animatable (android.graphics.drawable.Animatable)1 Drawable (android.graphics.drawable.Drawable)1 Uri (android.net.Uri)1 Handler (android.os.Handler)1 View (android.view.View)1 BaseControllerListener (com.facebook.drawee.controller.BaseControllerListener)1 ControllerListener (com.facebook.drawee.controller.ControllerListener)1 GenericDraweeHierarchy (com.facebook.drawee.generic.GenericDraweeHierarchy)1 RoundingParams (com.facebook.drawee.generic.RoundingParams)1 DraweeController (com.facebook.drawee.interfaces.DraweeController)1 SimpleDraweeView (com.facebook.drawee.view.SimpleDraweeView)1 StethoInterceptor (com.facebook.stetho.okhttp3.StethoInterceptor)1 Flowable (io.reactivex.Flowable)1 NonNull (io.reactivex.annotations.NonNull)1 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)1