Search in sources :

Example 1 with Cancellable

use of io.reactivex.functions.Cancellable in project RxJava by ReactiveX.

the class CancellableDisposableTest method disposeRace.

@Test
public void disposeRace() {
    for (int i = 0; i < 100; i++) {
        final AtomicInteger count = new AtomicInteger();
        Cancellable c = new Cancellable() {

            @Override
            public void cancel() throws Exception {
                count.getAndIncrement();
            }
        };
        final CancellableDisposable cd = new CancellableDisposable(c);
        Runnable r = new Runnable() {

            @Override
            public void run() {
                cd.dispose();
            }
        };
        TestHelper.race(r, r, Schedulers.io());
        assertEquals(1, count.get());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Cancellable(io.reactivex.functions.Cancellable) Test(org.junit.Test)

Example 2 with Cancellable

use of io.reactivex.functions.Cancellable in project RxJava by ReactiveX.

the class CancellableDisposableTest method cancelThrows.

@Test
public void cancelThrows() {
    final AtomicInteger count = new AtomicInteger();
    Cancellable c = new Cancellable() {

        @Override
        public void cancel() throws Exception {
            count.getAndIncrement();
            throw new TestException();
        }
    };
    CancellableDisposable cd = new CancellableDisposable(c);
    assertFalse(cd.isDisposed());
    List<Throwable> list = TestHelper.trackPluginErrors();
    try {
        cd.dispose();
        cd.dispose();
        TestHelper.assertUndeliverable(list, 0, TestException.class);
    } finally {
        RxJavaPlugins.reset();
    }
    assertTrue(cd.isDisposed());
    assertEquals(1, count.get());
}
Also used : TestException(io.reactivex.exceptions.TestException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Cancellable(io.reactivex.functions.Cancellable) Test(org.junit.Test)

Example 3 with Cancellable

use of io.reactivex.functions.Cancellable in project RxJava by ReactiveX.

the class CancellableDisposableTest method normal.

@Test
public void normal() {
    final AtomicInteger count = new AtomicInteger();
    Cancellable c = new Cancellable() {

        @Override
        public void cancel() throws Exception {
            count.getAndIncrement();
        }
    };
    CancellableDisposable cd = new CancellableDisposable(c);
    assertFalse(cd.isDisposed());
    cd.dispose();
    cd.dispose();
    assertTrue(cd.isDisposed());
    assertEquals(1, count.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Cancellable(io.reactivex.functions.Cancellable) Test(org.junit.Test)

Example 4 with Cancellable

use of io.reactivex.functions.Cancellable in project sqlbrite by square.

the class BriteContentResolver method createQuery.

/**
 * Create an observable which will notify subscribers with a {@linkplain Query query} for
 * execution. Subscribers are responsible for <b>always</b> closing {@link Cursor} instance
 * returned from the {@link Query}.
 * <p>
 * Subscribers will receive an immediate notification for initial data as well as subsequent
 * notifications for when the supplied {@code uri}'s data changes. Unsubscribe when you no longer
 * want updates to a query.
 * <p>
 * Since content resolver triggers are inherently asynchronous, items emitted from the returned
 * observable use the {@link Scheduler} supplied to {@link SqlBrite#wrapContentProvider}. For
 * consistency, the immediate notification sent on subscribe also uses this scheduler. As such,
 * calling {@link Observable#subscribeOn subscribeOn} on the returned observable has no effect.
 * <p>
 * Note: To skip the immediate notification and only receive subsequent notifications when data
 * has changed call {@code skip(1)} on the returned observable.
 * <p>
 * <b>Warning:</b> this method does not perform the query! Only by subscribing to the returned
 * {@link Observable} will the operation occur.
 *
 * @see ContentResolver#query(Uri, String[], String, String[], String)
 * @see ContentResolver#registerContentObserver(Uri, boolean, ContentObserver)
 */
@CheckResult
@NonNull
public QueryObservable createQuery(@NonNull final Uri uri, @Nullable final String[] projection, @Nullable final String selection, @Nullable final String[] selectionArgs, @Nullable final String sortOrder, final boolean notifyForDescendents) {
    final Query query = new Query() {

        @Override
        public Cursor run() {
            long startNanos = nanoTime();
            Cursor cursor = contentResolver.query(uri, projection, selection, selectionArgs, sortOrder);
            if (logging) {
                long tookMillis = NANOSECONDS.toMillis(nanoTime() - startNanos);
                log("QUERY (%sms)\n  uri: %s\n  projection: %s\n  selection: %s\n  selectionArgs: %s\n  " + "sortOrder: %s\n  notifyForDescendents: %s", tookMillis, uri, Arrays.toString(projection), selection, Arrays.toString(selectionArgs), sortOrder, notifyForDescendents);
            }
            return cursor;
        }
    };
    Observable<Query> queries = Observable.create(new ObservableOnSubscribe<Query>() {

        @Override
        public void subscribe(final ObservableEmitter<Query> e) throws Exception {
            final ContentObserver observer = new ContentObserver(contentObserverHandler) {

                @Override
                public void onChange(boolean selfChange) {
                    if (!e.isDisposed()) {
                        e.onNext(query);
                    }
                }
            };
            contentResolver.registerContentObserver(uri, notifyForDescendents, observer);
            e.setCancellable(new Cancellable() {

                @Override
                public void cancel() throws Exception {
                    contentResolver.unregisterContentObserver(observer);
                }
            });
            if (!e.isDisposed()) {
                // Trigger initial query.
                e.onNext(query);
            }
        }
    });
    return // 
    queries.observeOn(// 
    scheduler).compose(// Apply the user's query transformer.
    queryTransformer).to(QUERY_OBSERVABLE);
}
Also used : Query(com.squareup.sqlbrite3.SqlBrite.Query) Cancellable(io.reactivex.functions.Cancellable) Cursor(android.database.Cursor) ContentObserver(android.database.ContentObserver) CheckResult(android.support.annotation.CheckResult) NonNull(android.support.annotation.NonNull)

Aggregations

Cancellable (io.reactivex.functions.Cancellable)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Test (org.junit.Test)3 ContentObserver (android.database.ContentObserver)1 Cursor (android.database.Cursor)1 CheckResult (android.support.annotation.CheckResult)1 NonNull (android.support.annotation.NonNull)1 Query (com.squareup.sqlbrite3.SqlBrite.Query)1 TestException (io.reactivex.exceptions.TestException)1