Search in sources :

Example 36 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 37 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 38 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 39 with Subscription

use of rx.Subscription in project LookLook by xinghongfei.

the class TopNewsPrensenterImpl method getNewsList.

@Override
public void getNewsList(int t) {
    mITopNewsFragment.showProgressDialog();
    Subscription subscription = ApiManage.getInstence().getTopNewsService().getNews(t).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<NewsList>() {

        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
            mITopNewsFragment.hidProgressDialog();
            mITopNewsFragment.showError(e.toString());
        }

        @Override
        public void onNext(NewsList newsList) {
            mITopNewsFragment.hidProgressDialog();
            mITopNewsFragment.upListItem(newsList);
        }
    });
    addSubscription(subscription);
}
Also used : NewsList(com.looklook.xinghongfei.looklook.bean.news.NewsList) Subscription(rx.Subscription)

Example 40 with Subscription

use of rx.Subscription in project LookLook by xinghongfei.

the class ZhihuPresenterImpl method getLastZhihuNews.

@Override
public void getLastZhihuNews() {
    mZhihuFragment.showProgressDialog();
    Subscription subscription = ApiManage.getInstence().getZhihuApiService().getLastDaily().map(new Func1<ZhihuDaily, ZhihuDaily>() {

        @Override
        public ZhihuDaily call(ZhihuDaily zhihuDaily) {
            String date = zhihuDaily.getDate();
            for (ZhihuDailyItem zhihuDailyItem : zhihuDaily.getStories()) {
                zhihuDailyItem.setDate(date);
            }
            return zhihuDaily;
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<ZhihuDaily>() {

        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
            mZhihuFragment.hidProgressDialog();
            mZhihuFragment.showError(e.getMessage());
        }

        @Override
        public void onNext(ZhihuDaily zhihuDaily) {
            mZhihuFragment.hidProgressDialog();
            mCacheUtil.put(Config.ZHIHU, gson.toJson(zhihuDaily));
            mZhihuFragment.updateList(zhihuDaily);
        }
    });
    addSubscription(subscription);
}
Also used : ZhihuDaily(com.looklook.xinghongfei.looklook.bean.zhihu.ZhihuDaily) ZhihuDailyItem(com.looklook.xinghongfei.looklook.bean.zhihu.ZhihuDailyItem) Subscription(rx.Subscription) Func1(rx.functions.Func1)

Aggregations

Subscription (rx.Subscription)302 Test (org.junit.Test)82 List (java.util.List)75 CompositeSubscription (rx.subscriptions.CompositeSubscription)55 Action0 (rx.functions.Action0)45 ArrayList (java.util.ArrayList)41 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 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)17 Observable (rx.Observable)17 LinkedList (java.util.LinkedList)13 CommandStreamTest (com.netflix.hystrix.metric.CommandStreamTest)11 PlayList (io.github.ryanhoo.music.data.model.PlayList)11 MetaChangedEvent (io.hefuyi.listener.event.MetaChangedEvent)10 View (android.view.View)9 IOException (java.io.IOException)9 AndroidSchedulers (rx.android.schedulers.AndroidSchedulers)8 Intent (android.content.Intent)7