Skip to content

Instantly share code, notes, and snippets.

@joesteele
Last active August 29, 2015 14:06
Show Gist options
  • Save joesteele/ecab6f6ddf6a80dd9105 to your computer and use it in GitHub Desktop.
Save joesteele/ecab6f6ddf6a80dd9105 to your computer and use it in GitHub Desktop.
RxJava wrappers around Ply find methods
public Observable<List<Book>> books() {
return observablePlyFind(Book.class);
}
private static <T extends Model> Observable<List<T>> observableFromPlyQueryBuilder(final QueryBuilder<T> builder) {
return Observable.create(new Observable.OnSubscribe<List<T>>() {
@Override
public void call(Subscriber<? super List<T>> subscriber) {
subscriber.onNext(builder.find());
subscriber.onCompleted();
}
}).subscribeOn(Schedulers.io());
}
private static <T extends Model> Observable<List<T>> observablePlyFind(final Class<T> cls) {
return Observable.create(new Observable.OnSubscribe<List<T>>() {
@Override
public void call(Subscriber<? super List<T>> subscriber) {
subscriber.onNext(Ply.find(cls));
subscriber.onCompleted();
}
}).subscribeOn(Schedulers.io());
}
private static <T extends Model> Observable<T> observablePlyFind(final Class<T> cls, final long id) {
return Observable.create(new Observable.OnSubscribe<T>() {
@Override
public void call(Subscriber<? super T> subscriber) {
subscriber.onNext(Ply.find(cls, id));
subscriber.onCompleted();
}
}).subscribeOn(Schedulers.io());
}
Store.books()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<List<Book>>() {
@Override
public void call(List<Book> books) {
adapter.replaceWith(books);
}
});
@joesteele
Copy link
Author

In an app with sync'ing, Store.books() could handle the dance of database load, fetching books from the server, saving them to the db, and then returning the fresh books to the subscriber without the view needing to know anything about that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment