実際のアプリ開発で使ったRxを紹介 #RxJaNight

実際のアプリ開発で使ったRxを紹介 #RxJaNight

5c7e719825efd10bca38b800a8b0d685?s=128

Yuki Funakoshi

February 25, 2016
Tweet

Transcript

  1. 㹋ꥷך،فٔꟚ涪ד ⢪׏׋3Y׾稱➜ #RxJaNight

  2. ࣗݾ঺հ • Yuki Funakoshi (@bl_lia) • NYLE Inc. • Android

    / iOS Engineer
  3. جຊతͳ࿩

  4. ඇಉظॲཧͷίϯϙʔωϯτԽ Πϕϯτϕʔεϓϩάϥϛϯά

  5. Observable Subscriber subscribe send event

  6. Observable Observable Observable Subscriber filter map send event operator

  7. Observable Observable Subscriber send event send event

  8. None
  9. https://github.com/android10/Android-CleanArchitecture

  10. public interface AppSearchRepository { Observable<List<SearchResult>> search(String query); Observable<List<SearchResult>> searchMore(String query);

    Observable<List<Suggest>> suggest(String query); }
  11. Case 1 Observableͷ࡞੒

  12. • just(T value) • create(Observable.OnSubscribe<T> f)

  13. http://reactivex.io/documentation/operators/just.html

  14. Observable.just(sharedPreferences.getBoolean(KEY_ENABLE, true));

  15. http://reactivex.io/documentation/operators/create.html

  16. Observable.create(subscriber -> { final Gson gson = new Gson(); final

    String alertJson = sharedPreferences.getString(KEY_ALERT, ""); final Alert alert = gson.fromJson(alertJson, Alert.class); subscriber.onNext(alert); subscriber.onCompleted(); });
  17. Case 2 ॲཧͷՃ޻

  18. • filter(Func1) • startWith(Iterable) / startWith(T)

  19. http://reactivex.io/documentation/operators/filter.html

  20. Ϧετ͔Βআ֎ ɾγεςϜΞϓϦ ɾApplivΞϓϦ

  21. Observable.from(packageManager.getInstalledApplications(PackageManager.GET_META_DATA)) .filter(app -> { if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM)

    return false; if (applicationInfo.packageName.equals(application.getPackageName())) return false; return true; }) .toList();
  22. http://reactivex.io/documentation/operators/startwith.html

  23. ةحف

  24. recommendService.like(recommendId, isLike) .map(response -> response.recommend) .startWith(() -> { return fakeRecommend;

    });
  25. Case 3 API͔Βͷσʔλऔಘ

  26. • Retrofit • map(Func1) • doOnNext(Action1)

  27. { "result": true, "items": [{ "id": "aaaaaa", "title": "Item A"

    },{ "id": "bbbbbb", "title": "Item B" }] }
  28. None
  29. dependencies { compile 'io.reactivex:rxjava:1.1.1' compile 'com.squareup.retrofit:retrofit:2.0.0-beta2' compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2' }

  30. class Item { public final String id; public final String

    title; public Item(String id, String title) { this.id = id; this.title = title; } } class ItemResponse { public final boolean result; public final List<Item> items; public ItemResponse(boolean result, List<Item> items) { this.result = result; this.items = items; } } interface ItemService { @GET("/items") Observable<ItemResponse> items(); }
  31. public void getItems() { final Retrofit retrofit = new Retrofit.Builder().baseUrl("http://hogehoge.com")

    .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build(); final ItemService service = retrofit.create(ItemService.class); service.items() .map(itemResponse -> itemResponse.items); }
  32. http://reactivex.io/documentation/operators/map.html

  33. Factory Class cached? Disk Data Store API Data Store Disk

    Data Cache API
  34. Factory Class cached? Disk Data Store API Data Store Disk

    Data Cache API
  35. Factory Class cached? Disk Data Store API Data Store Disk

    Data Cache API
  36. Factory Class cached? Disk Data Store API Data Store Disk

    Data Cache API
  37. http://reactivex.io/documentation/operators/do.html

  38. interface ItemCache { void addItems(List<Item> items); } public void getItems(ItemCache

    cache) { final Retrofit retrofit = new Retrofit.Builder().baseUrl("http://hogehoge.com") .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build(); final ItemService service = retrofit.create(ItemService.class); service.items() .map(itemResponse -> itemResponse.items) .doOnNext(items -> cache.addItems(items)); }
  39. ׉ך➭

  40. • Τϥʔॲཧܥ • subscriber.onError(Throwable) • OnErrorThrowable.from(Throwable) • Observable.onError(Throwable) • onErrorResumeNext(Observable)

    • onErrorReturn(Func1)
  41. • ςετ·ΘΓ • TestSubscriber<T> • assertCompleted() / assertNotCompleted() • assertValue(T

    value) / assertNoValues() • assertError(java.lang.Throwable throwable) / assertNoErrors()