Search in sources :

Example 11 with Subscriber

use of rx.Subscriber in project sqlbrite by square.

the class QueryToListOperator method call.

@Override
public Subscriber<? super SqlBrite.Query> call(final Subscriber<? super List<T>> subscriber) {
    return new Subscriber<SqlBrite.Query>(subscriber) {

        @Override
        public void onNext(SqlBrite.Query query) {
            try {
                Cursor cursor = query.run();
                if (cursor == null || subscriber.isUnsubscribed()) {
                    return;
                }
                List<T> items = new ArrayList<>(cursor.getCount());
                try {
                    while (cursor.moveToNext()) {
                        items.add(mapper.call(cursor));
                    }
                } finally {
                    cursor.close();
                }
                if (!subscriber.isUnsubscribed()) {
                    subscriber.onNext(items);
                }
            } catch (Throwable e) {
                Exceptions.throwIfFatal(e);
                onError(OnErrorThrowable.addValueAsLastCause(e, query.toString()));
            }
        }

        @Override
        public void onCompleted() {
            subscriber.onCompleted();
        }

        @Override
        public void onError(Throwable e) {
            subscriber.onError(e);
        }
    };
}
Also used : Subscriber(rx.Subscriber) ArrayList(java.util.ArrayList) OnErrorThrowable(rx.exceptions.OnErrorThrowable) Cursor(android.database.Cursor)

Example 12 with Subscriber

use of rx.Subscriber in project sqlbrite by square.

the class QueryToOneOperator method call.

@Override
public Subscriber<? super SqlBrite.Query> call(final Subscriber<? super T> subscriber) {
    return new Subscriber<SqlBrite.Query>(subscriber) {

        @Override
        public void onNext(SqlBrite.Query query) {
            try {
                boolean emit = false;
                T item = null;
                Cursor cursor = query.run();
                if (cursor != null) {
                    try {
                        if (cursor.moveToNext()) {
                            item = mapper.call(cursor);
                            emit = true;
                            if (cursor.moveToNext()) {
                                throw new IllegalStateException("Cursor returned more than 1 row");
                            }
                        }
                    } finally {
                        cursor.close();
                    }
                }
                if (!subscriber.isUnsubscribed()) {
                    if (emit) {
                        subscriber.onNext(item);
                    } else if (emitDefault) {
                        subscriber.onNext(defaultValue);
                    } else {
                        // Account upstream for the lack of downstream emission.
                        request(1L);
                    }
                }
            } catch (Throwable e) {
                Exceptions.throwIfFatal(e);
                onError(OnErrorThrowable.addValueAsLastCause(e, query.toString()));
            }
        }

        @Override
        public void onCompleted() {
            subscriber.onCompleted();
        }

        @Override
        public void onError(Throwable e) {
            subscriber.onError(e);
        }
    };
}
Also used : Subscriber(rx.Subscriber) OnErrorThrowable(rx.exceptions.OnErrorThrowable) Cursor(android.database.Cursor)

Example 13 with Subscriber

use of rx.Subscriber in project Rutgers-Course-Tracker by tevjef.

the class ChooserPresenterImpl method loadSystemMessage.

@Override
public void loadSystemMessage() {
    cancePreviousSubscription();
    Subscriber<SystemMessage> mSubscriber = new Subscriber<SystemMessage>() {

        @Override
        public void onCompleted() {
        //Noop
        }

        @Override
        public void onError(Throwable e) {
        //Noop
        }

        @Override
        public void onNext(SystemMessage systemMessage) {
            if (getView() != null) {
                getView().showMessage(systemMessage);
            }
        }
    };
    mSubsciption = mRetroRutgers.getSystemMessage().doOnSubscribe(new Action0() {

        @Override
        public void call() {
            isLoading = true;
        }
    }).doOnTerminate(new Action0() {

        @Override
        public void call() {
            isLoading = false;
        }
    }).subscribeOn(mBackgroundThread).observeOn(mMainThread).subscribe(mSubscriber);
}
Also used : SystemMessage(com.tevinjeffrey.rutgersct.rutgersapi.model.SystemMessage) Action0(rx.functions.Action0) Subscriber(rx.Subscriber)

Example 14 with Subscriber

use of rx.Subscriber in project Shuttle by timusus.

the class QueryToListOperator method call.

@Override
public Subscriber<? super SqlBrite.Query> call(final Subscriber<? super List<T>> subscriber) {
    return new Subscriber<SqlBrite.Query>(subscriber) {

        @Override
        public void onNext(SqlBrite.Query query) {
            try {
                Cursor cursor = query.run();
                if (cursor == null) {
                    return;
                }
                List<T> items = new ArrayList<>(cursor.getCount());
                try {
                    for (int i = 1; cursor.moveToNext() && !subscriber.isUnsubscribed(); i++) {
                        T item = mapper.call(cursor);
                        if (item == null) {
                            throw new NullPointerException("Mapper returned null for row " + i);
                        }
                        items.add(item);
                    }
                } finally {
                    cursor.close();
                }
                if (!subscriber.isUnsubscribed()) {
                    subscriber.onNext(items);
                }
            } catch (Throwable e) {
                Exceptions.throwIfFatal(e);
                onError(OnErrorThrowable.addValueAsLastCause(e, query.toString()));
            }
        }

        @Override
        public void onCompleted() {
            subscriber.onCompleted();
        }

        @Override
        public void onError(Throwable e) {
            subscriber.onError(e);
        }
    };
}
Also used : Subscriber(rx.Subscriber) SqlBrite(com.squareup.sqlbrite.SqlBrite) ArrayList(java.util.ArrayList) OnErrorThrowable(rx.exceptions.OnErrorThrowable) Cursor(android.database.Cursor)

Example 15 with Subscriber

use of rx.Subscriber in project Shuttle by timusus.

the class QueryToOneOperator method call.

@Override
public Subscriber<? super SqlBrite.Query> call(final Subscriber<? super T> subscriber) {
    return new Subscriber<SqlBrite.Query>(subscriber) {

        @Override
        public void onNext(SqlBrite.Query query) {
            try {
                boolean emit = false;
                T item = null;
                Cursor cursor = query.run();
                if (cursor != null) {
                    try {
                        if (cursor.moveToNext()) {
                            item = mapper.call(cursor);
                            emit = true;
                            if (cursor.moveToNext()) {
                                throw new IllegalStateException("Cursor returned more than 1 row");
                            }
                        }
                    } finally {
                        cursor.close();
                    }
                }
                if (!subscriber.isUnsubscribed()) {
                    if (emit) {
                        subscriber.onNext(item);
                    } else if (emitDefault) {
                        subscriber.onNext(defaultValue);
                    } else {
                        // Account upstream for the lack of downstream emission.
                        request(1L);
                    }
                }
            } catch (Throwable e) {
                Exceptions.throwIfFatal(e);
                onError(OnErrorThrowable.addValueAsLastCause(e, query.toString()));
            }
        }

        @Override
        public void onCompleted() {
            subscriber.onCompleted();
        }

        @Override
        public void onError(Throwable e) {
            subscriber.onError(e);
        }
    };
}
Also used : Subscriber(rx.Subscriber) SqlBrite(com.squareup.sqlbrite.SqlBrite) OnErrorThrowable(rx.exceptions.OnErrorThrowable) Cursor(android.database.Cursor)

Aggregations

Subscriber (rx.Subscriber)48 List (java.util.List)16 Observable (rx.Observable)15 IOException (java.io.IOException)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 Action1 (rx.functions.Action1)10 Schedulers (rx.schedulers.Schedulers)10 Context (android.content.Context)8 ArrayList (java.util.ArrayList)8 OnSubscribe (rx.Observable.OnSubscribe)8 TestCircuitBreaker (com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker)7 RxException (com.yydcdut.note.model.rx.exception.RxException)7 Notification (rx.Notification)7 TestSubscriber (rx.observers.TestSubscriber)7 Cursor (android.database.Cursor)5 ContextLife (com.yydcdut.note.injector.ContextLife)5 Map (java.util.Map)5 Inject (javax.inject.Inject)5 Subscription (rx.Subscription)5 Action0 (rx.functions.Action0)5