Last active
August 29, 2015 14:06
-
-
Save joesteele/ecab6f6ddf6a80dd9105 to your computer and use it in GitHub Desktop.
RxJava wrappers around Ply find methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Store.books() | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(new Action1<List<Book>>() { | |
@Override | |
public void call(List<Book> books) { | |
adapter.replaceWith(books); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.