Upgrade to Pro — share decks privately, control downloads, hide ads and more …

LiveData with RxJava2

LiveData with RxJava2

shibuya.apk #16 の発表資料です。
https://shibuya-apk.connpass.com/event/59671/

Tatsuya Kitagawa

June 22, 2017
Tweet

More Decks by Tatsuya Kitagawa

Other Decks in Programming

Transcript

  1. PCTFSWF -JGFDZDMF0XOFS  0CTFSWFS5 LiveData<String> textLiveData = viewModel.getMessage(); textLiveData.observe(this, text

    -> { // ͜͜͸ΞΫςΟϒͳͱ͖͚ͩݺ͹ΕΔ textView.setText(text); }); •LifecycleOwnerʹ͸ී௨Activity΍Fragment •UI͕ΞΫςΟϒͳঢ়ଶʢSTARTED, RESUMEDʣͰͷΈɺ
 σʔλͷมߋΛड͚औΔ͜ͱ͕Ͱ͖Δ
  2. GSPN1VCMJTIFS 1VCMJTIFS ʢൈਮʣ public static <T> LiveData<T> fromPublisher(final Publisher<T> publisher)

    { MutableLiveData<T> liveData = new MutableLiveData<>(); final WeakReference<MutableLiveData<T>> liveDataRef = new WeakReference<>(liveData); publisher.subscribe(new Subscriber<T>() { @Override public void onSubscribe(Subscription s) { s.request(Long.MAX_VALUE); } @Override public void onNext(final T t) { final LiveData<T> liveData = liveDataRef.get(); if (liveData != null) { liveData.postValue(t); } }
  3. GSPN1VCMJTIFS 1VCMJTIFS ʢൈਮʣ public static <T> LiveData<T> fromPublisher(final Publisher<T> publisher)

    { // … publisher.subscribe(new Subscriber<T>() { // … @Override public void onError(Throwable t) { // Errors should be handled upstream, so propagate as a crash. throw new RuntimeException(t); } @Override public void onComplete() { } }); return liveData; }
  4. -JWF%BUBͷΤϥʔॲཧʢྫʣ // TODOΛDB͔Βऔಘ͢Δαϯϓϧ // ্ྲྀͰΤϥʔ͕͋ͬͨͱ͖ʹͦͷ··ྫ֎Λ౤͛Δ LiveData<List<Todo>> getTodos(TodoStatus status) { Flowable<List<Todo>>

    list = todoDb.todoDao().findByStatus(status) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()); return LiveDataReactiveStreams.fromPublisher(list); }
  5. -JWF%BUBͷΤϥʔॲཧʢྫʣ public class LoadState<T> { private boolean loading; private T

    data; private Throwable error; public LoadState(boolean loading, @Nullable T data, @Nullable Throwable error) { this.loading = loading; this.data = data; this.error = error; }
  6. -JWF%BUBͷΤϥʔॲཧʢྫʣ LiveData<LoadState<List<Todo>>> getTodos(TodoStatus status) { Flowable<LoadState<List<Todo>>> state = todoDb.todoDao().findByStatus(status) .map(LoadState::data)

    // औಘ੒ޭ .onErrorReturn(LoadState::error) // Τϥʔ .startWith(LoadState.loading()) // ϩʔσΟϯά .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()); return LiveDataReactiveStreams.fromPublisher(state); }
  7. ࣦഊɾ׬ྃΛද͢ΦϒδΣΫτ public interface AddTodoState { final class None implements AddTodoState

    {} final class Loading implements AddTodoState {} final class Error implements AddTodoState { final Throwable throwable; public Error(Throwable throwable) { this.throwable = throwable; } public Throwable getThrowable() { return throwable; } } final class Complete implements AddTodoState {} }
  8. $PNQMFUBCMFUP-JWF%BUB MutableLiveData<AddTodoState> addTodoState = new MutableLiveData<>(); Completable completable = todoRepository.addTodo(todo);

    Disposable disposable = completable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .toSingle(AddTodoState.Complete::new) .toObservable() .cast(AddTodoState.class) .onErrorReturn(AddTodoState.Error::new) .startWith(new AddTodoState.Loading()) .subscribe(addTodoState::setValue);
  9. ࢀߟϦϯΫ w αϯϓϧίʔυ
 IUUQTHJUIVCDPNZBUBUTVBOESPJEBSDIUPEPTBNQMF w ެࣜͷαϯϓϧϦϙδτϦ
 IUUQTHJUIVCDPNHPPHMFTBNQMFTBOESPJEBSDIJUFDUVSFDPNQPOFOUT USFFNBTUFS(JUIVC#SPXTFS4BNQMF w (PPHMF*0ͷηογϣϯ


    "SDIJUFDUVSF$PNQPOFOUT4PMWJOHUIF-JGFDZDMF1SPCMFN (PPHMF*0  :PV5VCF
 IUUQTXXXZPVUVCFDPNXBUDI WC&,/J+0S/T w ެࣜυΩϡϝϯτ
 -JWF%BUBc"OESPJE%FWFMPQFST
 IUUQTEFWFMPQFSBOESPJEDPNUPQJDMJCSBSJFTBSDIJUFDUVSFMJWFEBUBIUNM