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

Rx入門

 Rx入門

Reactive Extensions入門。社内勉強会用。

Rei Matsushita

August 10, 2016
Tweet

More Decks by Rei Matsushita

Other Decks in Technology

Transcript

  1. 0QFSBUPSͰ஋ΛՃ޻͢Δ w ྲྀΕͯ͘ΔσʔλΛՃ޻͢Δྫʢ+BWBʣ w ΦϖϨʔλͷҰཡIUUQSFBDUJWFYJPEPDVNFOUBUJPOPQFSBUPSTIUNM List<Integer> list = Arrays.asList(1, 2,

    3, 4, 5, 6);
 Observable.from(list)
 .filter(v -> (v % 2) == 1)
 .map(v -> v * 2)
 .subscribe(System.out::println); 2 6 10
  2. 0CTFSWBCMFΛ߹੒͢Δ Observable<Integer> stream1 = Observable.from(Arrays.asList(1, 2, 3));
 Observable<Integer> stream2 =

    Observable.from(Arrays.asList(10, 20, 30));
 Observable.concat(stream1, stream2).subscribe(System.out::println); 1 2 3 10 20 30 Observable<Integer> stream1 = Observable.from(Arrays.asList(1, 2, 3));
 Observable<Integer> stream2 = Observable.from(Arrays.asList(10, 20, 30));
 Observable.zip(stream1, stream2, ((v1, v2) -> {
 return v1 + v2;
 })).subscribe(System.out::println); 11 22 33
  3. ΠϕϯτΛ0CTFSWBCMFͱͯ͠ѻ͏ w ͜͜·Ͱͷ಺༰͸-JTUΛ4USFBNͱͯ͠ѻ͚ͬͨͩ w ϢʔβʔͷΠϕϯτΛ0CTBSWBCMFͰྲྀ͢ private Subscription subscription;
 @Override
 public

    View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { … // ͜͜ͰϘλϯΛ؂ࢹͯ͠ΠϕϯτΛྲྀ͢StreamΛ࡞Δ. subscription = RxView.clicks(view.findViewById(R.id.button)).map(v -> 1).subscribe(v -> {
 System.out.println("ԡ͞Εͯ " + v + "͕ྲྀΕ͖ͯͨΑ");
 }); } @Override
 public void onDestroyView() {
 super.onDestroyView();
 subscription.unsubscribe(); // View͕ഁغ͞ΕΔλΠϛϯάͰStreamͷߪಡΛղআͯ͠؂ࢹΛղ͘
 subscription = null;
 }
  4. Ұ୴໭ͬͯ0CTFSWFSΠϯλʔϑΣʔεΛோΊΔ w 4USFBN͕ऴ୺ʹୡ͢ΔͱPO$PNQMFUFE͕૸ͬͯߪಡ͸VOTVCTDSJCF͞ΕΔ w 7JFXͷ৔߹͸ৗʹ؂ࢹ͞Ε͍ͯΔɺͭ·Γ4USFBN͸ແݶͰPO$PNQMFUFE͕૸Βͳ͍ͷͰࣗ෼ͰVOTVCTDSJCF͠ͳ͍ͱϦʔ Ϋ͢Δɻ public interface Observer<T> {


    
 /**
 * Notifies the Observer that the {@link Observable} has finished sending push-based notifications.
 * <p>
 * The {@link Observable} will not call this method if it calls {@link #onError}.
 */
 void onCompleted();
 
 /**
 * Notifies the Observer that the {@link Observable} has experienced an error condition.
 * <p>
 * If the {@link Observable} calls this method, it will not thereafter call {@link #onNext} or
 * {@link #onCompleted}.
 * 
 * @param e
 * the exception encountered by the Observable
 */
 void onError(Throwable e);
 
 /**
 * Provides the Observer with a new item to observe.
 * <p>
 * The {@link Observable} may call this method 0 or more times.
 * <p>
 * The {@code Observable} will not call this method again after it calls either {@link #onCompleted} or
 * {@link #onError}.
 * 
 * @param t
 * the item emitted by the Observable
 */
 void onNext(T t);
 
 }
  5. private val subscriptions = CompositeSubscription()
 override fun onCreateView(inflater: LayoutInflater, container:

    ViewGroup?, savedInstanceState: Bundle?): View {
 
 val view = inflater.inflate(R.layout.fragment_rx_sample, container, false)
 
 // ֤ೖྗཝͷObservableΛऔಘ
 val observableName = RxTextView.textChanges(view.findViewById(R.id.edit_order_name) as AppCompatEditText)
 val observablePostalCode = RxTextView.textChanges(view.findViewById(R.id.edit_order_postal_code) as AppCompatEditText)
 val observableAddressFirst = RxTextView.textChanges(view.findViewById(R.id.edit_order_address_first) as AppCompatEditText)
 val observableAddressSecond = RxTextView.textChanges(view.findViewById(R.id.edit_order_address_second) as AppCompatEditText)
 
 // ButtonΛऔಘ
 val submitButton = view.findViewById(R.id.button_rx_submit) as AppCompatButton
 submitButton.isEnabled = false
 
 // ObservableΛcombineLatestʹ৯Θ֤߲ͯ͠໨ͷͲΕ͔ʹมߋ͕͋ͬͨ৔߹͸Stream͕ྲྀΕΔΑ͏ʹ͢Δ
 // Ҿ਺ͷ࠷ޙͷAction1ͰྲྀΕ͖ͯͨ஋ΛνΣοΫͯ͢͠΂ͯೖྗࡁͳΒtrueΛྲྀ͢
 subscriptions.add(Observable.combineLatest(observableName,
 observablePostalCode,
 observableAddressFirst,
 observableAddressSecond,
 { v1, v2, v3, v4 ->
 (0 < v1.length && 0 < v2.length && 0 < v3.length && 0 < v4.length)
 }).subscribe({ isValid ->
 // ྲྀΕ͖ͯͨStreamʹै͍Ϙλϯͷ༗ޮ/ແޮΛ੾Γସ͑Δ
 submitButton.isEnabled = isValid
 }))
 
 return view
 }
 
 override fun onDestroyView() {
 super.onDestroyView()
 subscriptions.unsubscribe()
 } w αϯϓϧίʔυ͸,PUMJO IUUQRJJUBDPNSFJNJUFNTBDCFCFDCGFDBD
  6. "1*ͷϨεϙϯε0CTFSWBCMFͱͯ͠ѻ͏ w 3Y͸ඇಉظॲཧͱ૬ੑ͕͍͍ɻΞϓϦͰඇಉظͱ͍͑͹"1*ͷݺͼग़͠ɻ w "1*ΛݺΜͰͦͷϨεϙϯεΛ0CTFSWBCMFʹྲྀͯ͠ฦ͢ɻ w "1*΋TVCTDSJQUJPOͷ؅ཧ͸ͨ͠΄͏͕Α͍ɻ"1*ΛݺΜͰ݁ՌΛ7JFXʹදࣔ͢Δͱ͍͏Α͏ͳ͜ͱΛ͍ͯ͠Δ৔߹ɺ7JFX ͕ഁغ͞ΕΔλΠϛϯάͰVOTVCTDSJCF͠ͳ͍ͱ7JFXͷ/VMMࢀর͕ى͖Δʢ"1*ͷϦΫΤετதʹը໘͕ऴྃ͢ΔͳͲͯ͠ 7JFX͕ഁغ͞ΕΔέʔεʣՄೳੑ͕͋Δɻ w

    0CTFSWBCMFͳͷͰ౰વɺ߹੒΍ม׵ͳͲͷPQFSBUPS͕࢖͑Δɻຊͷ"1*Λ౤͛ऴΘͬͨΒ࣍ʹߦ͘ɺΈ͍ͨͳॲཧ΋ 0CTFSWBCMFʹม׵ͯ͠ɺ[JQͰ߹੒ͯ͠TVCTDSJCF͢Ε͹؆୯ʹॻ͚Δɻ // ͜Ε͸Sunnychatͷྫ // getCanvas͸APIΛୟ͍ͯObservable<CanvasEntity>Λฦ͢. subscriptions.add(canvasService.getCanvas(authToken, canvasIdentifier)
 .subscribeOn(Schedulers.newThread()) // ഑৴͸ผεϨουͰ
 .observeOn(AndroidSchedulers.mainThread()) // ߪಡ͸ϝΠϯεϨουͰ
 .subscribe({ // ͪ͜Β͸onNext
 onGetCanvasSuccess(it)
 }, { // ͪ͜Β͸onErrorɻωοτϫʔΫΤϥʔ΍200Ҏ֎ͷϨεϙϯεͷ৔߹͸഑৴ଆͰonError͕ݺ͹ΕΔ
 onGetCanvasFailure(it, canvasIdentifier)
 }))