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);
}
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);
}
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);
}
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);
}
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();
}
Aggregations