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

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

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

Yuki Funakoshi

February 25, 2016
Tweet

More Decks by Yuki Funakoshi

Other Decks in Programming

Transcript

  1. 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(); });
  2. { "result": true, "items": [{ "id": "aaaaaa", "title": "Item A"

    },{ "id": "bbbbbb", "title": "Item B" }] }
  3. 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(); }
  4. 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); }
  5. 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)); }
  6. • ςετ·ΘΓ • TestSubscriber<T> • assertCompleted() / assertNotCompleted() • assertValue(T

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