Search in sources :

Example 71 with Subscription

use of rx.Subscription in project realm-java by realm.

the class DetailsPresenter method onResume.

@Override
public void onResume() {
    // Show story details
    Subscription detailsSubscription = model.getStory(storyId).subscribe(new Action1<NYTimesStory>() {

        @Override
        public void call(NYTimesStory story) {
            view.hideLoader();
            view.showStory(story);
            view.setRead(story.isRead());
        }
    });
    // Mark story as read if screen is visible for 2 seconds
    Subscription timerSubscription = Observable.timer(2, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Long>() {

        @Override
        public void call(Long aLong) {
            model.markAsRead(storyId, true);
        }
    });
    subscriptions = new CompositeSubscription(detailsSubscription, timerSubscription);
}
Also used : NYTimesStory(io.realm.examples.newsreader.model.entity.NYTimesStory) CompositeSubscription(rx.subscriptions.CompositeSubscription) CompositeSubscription(rx.subscriptions.CompositeSubscription) Subscription(rx.Subscription)

Example 72 with Subscription

use of rx.Subscription in project realm-java by realm.

the class GotchasActivity method testSubscribeOn.

/**
     * Shows how to be careful with `subscribeOn()`
     */
private Subscription testSubscribeOn() {
    Subscription subscribeOn = realm.asObservable().map(new Func1<Realm, Person>() {

        @Override
        public Person call(Realm realm) {
            return realm.where(Person.class).findAllSorted("name").get(0);
        }
    }).subscribeOn(//
    Schedulers.io()).subscribe(new Action1<Person>() {

        @Override
        public void call(Person person) {
        // Do nothing
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            showStatus("subscribeOn: " + throwable.toString());
        }
    });
    // Use Realms Async API instead
    Subscription asyncSubscribeOn = realm.where(Person.class).findAllSortedAsync("name").get(0).<Person>asObservable().subscribe(new Action1<Person>() {

        @Override
        public void call(Person person) {
            showStatus("subscribeOn/async: " + person.getName() + ":" + person.getAge());
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            showStatus("subscribeOn/async: " + throwable.toString());
        }
    });
    return new CompositeSubscription(subscribeOn, asyncSubscribeOn);
}
Also used : CompositeSubscription(rx.subscriptions.CompositeSubscription) CompositeSubscription(rx.subscriptions.CompositeSubscription) Subscription(rx.Subscription) Realm(io.realm.Realm) Person(io.realm.examples.rxjava.model.Person)

Example 73 with Subscription

use of rx.Subscription in project realm-java by realm.

the class GotchasActivity method onResume.

@Override
protected void onResume() {
    super.onResume();
    Subscription distinctSubscription = testDistinct();
    Subscription bufferSubscription = testBuffer();
    Subscription subscribeOnSubscription = testSubscribeOn();
    // Trigger updates
    realm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            realm.where(Person.class).findAllSorted("name", Sort.ASCENDING).get(0).setAge(new Random().nextInt(100));
        }
    });
    subscription = new CompositeSubscription(distinctSubscription, bufferSubscription, subscribeOnSubscription);
}
Also used : Random(java.util.Random) CompositeSubscription(rx.subscriptions.CompositeSubscription) CompositeSubscription(rx.subscriptions.CompositeSubscription) Subscription(rx.Subscription) Realm(io.realm.Realm)

Example 74 with Subscription

use of rx.Subscription in project reark by reark.

the class GitHubRepositoryFetcher method fetchGitHubRepository.

private void fetchGitHubRepository(final int repositoryId) {
    Log.d(TAG, "fetchGitHubRepository(" + repositoryId + ")");
    if (isOngoingRequest(repositoryId)) {
        Log.d(TAG, "Found an ongoing request for repository " + repositoryId);
        return;
    }
    final String uri = getUniqueId(repositoryId);
    Subscription subscription = createNetworkObservable(repositoryId).subscribeOn(Schedulers.computation()).doOnSubscribe(() -> startRequest(uri)).doOnError(doOnError(uri)).doOnCompleted(() -> completeRequest(uri)).subscribe(gitHubRepositoryStore::put, e -> Log.e(TAG, "Error fetching GitHub repository " + repositoryId, e));
    addRequest(repositoryId, subscription);
}
Also used : Subscription(rx.Subscription)

Example 75 with Subscription

use of rx.Subscription in project requery by requery.

the class RxTest method testQuerySelfObservableMap.

@Test
public void testQuerySelfObservableMap() throws Exception {
    final AtomicInteger count = new AtomicInteger();
    Subscription subscription = data.select(Person.class).limit(2).get().toSelfObservable().flatMap(new Func1<RxResult<Person>, Observable<Person>>() {

        @Override
        public Observable<Person> call(RxResult<Person> persons) {
            return persons.toObservable();
        }
    }).subscribe(new Action1<Person>() {

        @Override
        public void call(Person persons) {
            count.incrementAndGet();
        }
    });
    data.insert(randomPerson()).toBlocking().value();
    data.insert(randomPerson()).toBlocking().value();
    assertEquals(3, count.get());
    subscription.unsubscribe();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Subscription(rx.Subscription) Func1(rx.functions.Func1) Person(io.requery.test.model.Person) RxResult(io.requery.rx.RxResult) Test(org.junit.Test)

Aggregations

Subscription (rx.Subscription)275 Test (org.junit.Test)82 List (java.util.List)66 Action0 (rx.functions.Action0)45 ArrayList (java.util.ArrayList)40 CompositeSubscription (rx.subscriptions.CompositeSubscription)40 CountDownLatch (java.util.concurrent.CountDownLatch)38 Func1 (rx.functions.Func1)24 AtomicReference (java.util.concurrent.atomic.AtomicReference)20 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)19 Observable (rx.Observable)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)17 LinkedList (java.util.LinkedList)13 CommandStreamTest (com.netflix.hystrix.metric.CommandStreamTest)11 PlayList (io.github.ryanhoo.music.data.model.PlayList)11 View (android.view.View)10 MetaChangedEvent (io.hefuyi.listener.event.MetaChangedEvent)10 Bundle (android.os.Bundle)9 IOException (java.io.IOException)9 AndroidSchedulers (rx.android.schedulers.AndroidSchedulers)9