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

RxJava for Looky

RxJava for Looky

RxJava beginner's guide

Joonhyeok Im

March 31, 2017
Tweet

Other Decks in Technology

Transcript

  1. ࠺زӝ asynchronous زӝ synchronous “ղо ૑Ә ߄ࢁפө ੓׮о ઴ԋ.” ਃ୒җ

    Ѿҗо زदী ੌযա૑ ঋ਺ “ઑӘ݅ ӝ׮۰. غݶ ঴ܲ ઴ԋ.” ਃ୒җ Ѿҗо زदী ੌযթ ࢸ҅о рױೞҊ ૒ҙ੸੐ ࢸ҅о ࠂ੟ೣ Ѿҗо ઱য૕ ٸө૑ ইޖѪب ޅೞҊ ؀ӝ ইޖܻ ӡযઉب ׮ܲ ੘সਸ ೡ ࣻ ੓਺ ബਯ੸ੋ ੗ਗҙܻо оמ ޖѢ਍ ؘ੉ఠ ୊ܻ, ֎౟ਕ௼ I/O рױೠ ୊ܻ, ചݶ ୊ܻ blocking non-blocking बച https://slipp.net/questions/367
  2. Event Iterable (pull) Observable (push) Retrieve data T next() onNext(T)

    Discover error throws Exception onError(Exception) Complete !hasNext() onCompleted() Single thread Single/Multi thread Synchronous Asynchronous
  3. $ pbpaste | highlight --syntax=java -O rtf --style molokai |

    pbcopy InputStream fin = new FileInputStream("1209027132159_1.jpg"); OutputStream fos = new FileOutputStream("copy.jpg"); while(true) { int data = fin.read(); if(data == -1) { System.out.println("EOF"); break; } fos.write(data); } fin.close(); fos.close(); C:\> type autoexec.bat | more
  4. ࢤࢿ Create — ૒੽੸ੋ ௏٘ ҳഅਸ ా೧ ২੷ߡ ݫࢲ٘ܳ ഐ୹ೞৈ

    Observableਸ ࢤࢿೠ׮ Defer — ২੷ߡо ҳةೞӝ ੹ө૑ח Observable ࢤࢿਸ ૑োೞҊ ҳة੉ द੘غݶ ২੷ߡ ߹ ۽ ࢜۽਍ Observableਸ ࢤࢿೠ׮ Empty/Never/Throw — ই઱ ੿ഛೞҊ ઁೠػ ೯زਸ ೞח Observableਸ ࢤࢿೠ׮ From — ׮ܲ ё୓ա ੗ܐ ҳઑܳ Observable۽ ߸ജೠ׮ Interval — ౠ੿ दр߹۽ োࣘػ ੿ࣻഋਸ ߓ୹ೞח Observableਸ ࢤࢿೠ׮ Just — ё୓ ೞա ژח ё଻૘೤ਸ Observable۽ ߸ജೠ׮. ߸ജػ Observable਷ ਗࠄ ё୓ ٜਸ ߊ೯ೠ׮ Range — োࣘػ ߧਤ(Range)੄ ੿ࣻܳ ߊ೯ೞח Observableਸ ࢤࢿೠ׮ Repeat — ౠ੿ ೦ݾ੉ա োࣘػ ೦ݾٜਸ ߈ࠂ੸ਵ۽ ߓ୹ೞח Observableਸ ࢤࢿೠ׮ Start — ೣࣻ੄ प೯ Ѿҗܳ ߓ୹ೞח Observableਸ ࢤࢿೠ׮ Timer — ૑੿ػ दр੉ ૑աҊ դ റ ೦ݾਸ ೞա ߓ୹ೞח Observableਸ ࢤࢿೠ׮ ߸ജ Buffer — Observable۽ࠗఠ ੿ӝ੸ਵ۽ ೦ݾٜਸ ࣻ૘ೞҊ ޘ਺ਵ۽ ݅ٚ റী ޘ਺ উী ੓ח ೦ ݾٜਸ ೠߣী ೞաঀ ߓ୹ೞ૑ ঋҊ ࣻ૘ػ ޘ਺ ױਤ۽ ߓ୹ೠ׮ FlatMap — ೞա੄ Observable੉ ߊ೯ೞח ೦ݾٜਸ ৈ۞ѐ੄ Observable۽ ߸ജೞҊ, ೦ݾٜ ੄ ߓ୹ਸ ର۹ର۹ ઴ ࣁਕ ೞա੄ Observable۽ ੹׳ೠ׮ GroupBy — ਗࠄ Observable੉ ߓ୹ೞח ೦ݾٜਸ ః(Key) ߹۽ ޘ਷ റ Observableী ׸ח ׮. ੉ۧѱ ః ߹۽ ٜ݅য૓ Observableٜ਷ ੗ӝо ׸Ҋ ੓ח ޘ਺੄ ೦ݾٜਸ ߓ୹ೠ׮ Map — Observable੉ ߓ୹ೠ ೦ݾী ೣࣻܳ ੸ਊೠ׮ Scan — Observable੉ ߓ୹ೠ ೦ݾী োࣘ੸ਵ۽ ೣࣻܳ ੸ਊೞҊ प೯ೠ റ ࢿҕ੸ਵ۽ प೯ػ ೣࣻ੄ ܻఢ чਸ ߊ೯ೠ׮ Window — ੿ӝ੸ਵ۽ Observable੄ ೦ݾٜਸ ؊ ੘਷ ױਤ੄ Observable ਦب਋۽ ա׀ റী, ೠߣী ೞաঀ ೦ݾٜਸ ߊ೯ೞח ؀न ੘ѱ աׇ૓ ਦب਋ ױਤ۽ ೦ݾٜਸ ߓ୹ೠ׮ ೙ఠ݂ Debounce — Observable੄ दр ൒ܴ੉ ૑ࣘغח ࢚కীࢲ ׮ܲ ೦ݾٜ਷ ߓ୹ೞ૑ ঋҊ ౠ੿ दр ݃׮ Ӓ द੼ী ઓ੤ೞח ೦ݾ ೞաܳ Observable۽ࠗఠ ߓ୹ೠ׮ Distinct — Observable੉ ߓ୹ೞח ೦ݾٜ ઺ ઺ࠂਸ ઁѢೠ ೦ݾٜਸ ߓ୹ೠ׮ ElementAt — Obserableীࢲ nߣ૩ ೦ݾ݅ ߓ୹ೠ׮ Filter — పझ౟ ઑѤਸ ݅઒ೞח ೦ݾٜ݅ ߓ୹ೠ׮ First — ݔ ୐ ߣ૩ ೦ݾ ژח ઑѤਸ ݅઒ೞח ୐ ߣ૩ ೦ݾ݅ ߓ୹ೠ׮ IgnoreElements — ೦ݾٜਸ ߓ୹ೞ૑ח ঋҊ ઙܐ ঌܿ਷ ࠁմ׮ Last — Observable੄ ݃૑݄ ೦ݾ݅ ߓ୹ೠ׮ Sample — ౠ੿ दр рѺਵ۽ ୭Ӕী Observable੉ ߓ୹ೠ ೦ݾٜਸ ߓ୹ೠ׮ Skip — Observable੉ ߓ୹ೠ ୊਺ nѐ੄ ೦ݾٜਸ ऀӟ׮ SkipLast — Observable੉ ߓ୹ೠ ݃૑݄ nѐ੄ ೦ݾٜਸ ऀӟ׮ Take — Observable੉ ߓ஑ೠ ୊਺ nѐ੄ ೦ݾٜ݅ ߓ୹ೠ׮ TakeLast — Observable੉ ߓ୹ೠ ݃૑݄ nѐ੄ ೦ݾٜ݅ ߓ୹ೠ׮ Ѿ೤ And/Then/When — ف ѐ ੉࢚੄ Observableٜ੉ ߓ୹ೠ ೦ݾٜਸ 'Pattern'җ 'Plan' ઺҅੗ܳ ੉ਊ ೧ࢲ Ѿ೤ೠ׮ CombineLatest — ف ѐ੄ Observable ઺ ೞաо ೦ݾਸ ߓ୹ೡ ٸ ߓ୹ػ ݃૑݄ ೦ݾҗ ׮ܲ ೠ Observable੉ ߓ୹ೠ ೦ݾਸ Ѿ೤ೠ റ ೣࣻܳ ੸ਊೞৈ प೯ റ प೯ػ Ѿҗܳ ߓ୹ೠ׮ Join — A Observableҗ B Observable੉ ߓ୹ೠ ೦ݾٜਸ Ѿ೤ೞחؘ, ੉ٸ B Observable਷ ߓ୹ ೠ ೦ݾ੉ ఋ੐ ਦب਋ܳ о૑Ҋ ੓Ҋ ੉ ఋ੐ ਦب਋о ৌܽ زউ A Observable਷ ೦ݾ੄ ߓ୹ਸ ҅ࣘ ೠ׮. Join ো࢑੗ח B Observable੄ ೦ݾਸ ߓ୹ೞҊ ߓ୹ػ ೦ݾ਷ ఋ੐ ਦب਋ܳ द੘दఅ׮. ఋ੐ ਦب਋о ৌ۰ ੓ח زউ A Observable਷ ੗न੄ ೦ݾٜਸ ҅ࣘ ߓ୹ೞৈ ੉ ف ೦ݾٜਸ Ѿ೤ೠ׮ Merge — ࠂࣻ ѐ੄ Observableٜ੉ ߓ୹ೞח ೦ݾٜਸ ݠ૑दெ ೞա੄ Observable۽ ݅ٚ׮ StartWith — ࣗझ Observable੉ ೦ݾਸ ߓ୹ೞӝ ੹ী ׮ܲ ೦ݾٜਸ খী ୶оೠ റ ߓ୹ೠ׮ Switch — Observableٜਸ ߓ୹ೞח Observableਸ यӖ Observable۽ ߸ജೞ׮. ߸ജػ यӖ Observable਷ ߸ജ ੹ ࣗझ Observableٜ੉ ߓ୹ೠ ೦ݾٜਸ ߓ୹ೠ׮ Zip — ݺदೠ ೣࣻܳ ా೧ ৈ۞ Observableٜ੉ ߓ୹ೠ ೦ݾٜਸ Ѿ೤ೞҊ ೣࣻ੄ प೯ Ѿҗܳ ߓ୹ೠ ׮ য়ܨ ୊ܻ ো࢑੗, Observable ਬ౰ܻ౭ ো࢑੗, ઑѤҗ ࠛܽ ো࢑੗(Boolean), ࣻ೟җ ૘҅ ো࢑੗, ৉঑(Backpressure) ো࢑੗, ... http://reactivex.io/documentation/ko/operators.html
  5. ҃Ҋ ૑Әࠗఠ աয়ח ࣗझ௏٘ח ৮߷ೠ Ѫ੉ ইפ޲۽, ࠗ઒ೠ ࠗ࠙਷ झझ۽

    ੜ Ҋ୛ࢲ উ੿੸ੋ ೐۽Ӓې߁ਸ ࠗఌܻ٘ݴ, ই਎۞ ߊ಴੗ীѱب ೠ ࣻ оܰ୛઱दӡ Icon Author: http://www.flaticon.com/authors/gregor-cresnar
  6. TextViewী OnTextChangedListenerܳ ׳Ҋ, ੑ۱ػ ఫझ౟۽ HTTP ௿ۄ੉঱౟ܳ ా೧ Suggestion ఫझ౟

    ܻझ౟ܳ ߉ইৡ׮. ߉ইৡ ܻझ౟ܳ ߃ী ੓ח RecyclerView੄ Adapterী ߈৔ೠ׮.
  7. without Rx with Rx TextViewী OnTextChangedListenerܳ ׳Ҋ, ੑ۱ػ ఫझ౟۽ HTTP

    ௿ۄ੉঱౟ܳ ా೧ Suggestion ఫझ౟ ܻझ౟ܳ ߉ইৡ׮. public interface MyRestService { @GET("suggest/{keyword}") Call<List<String>> listSuggestions( @Path("keyword") String keyword); } @OnTextChanged(R.id.keyword) public void keywordChanged(String keyword) { Call<List<String>> suggestions = service.listSuggestions(keyword); } public interface MyRestService { @GET("suggest/{keyword}") Observable<List<String>> listSuggestions( @Path("keyword") String keyword); } RxTextView.textChanges(R.id.keyword) .map(CharSequence::toString) .flatMap(text -> service.listSuggestions(text));
  8. without Rx with Rx TextViewী OnTextChangedListenerܳ ׳Ҋ, ੑ۱ػ ఫझ౟۽ HTTP

    ௿ۄ੉঱౟ܳ ా೧ Suggestion ఫझ౟ ܻझ౟ܳ ߉ইৡ׮. ߉ইৡ ܻझ౟ܳ ߃ী ੓ח RecyclerView੄ Adapterী ߈৔ೠ׮. public interface MyRestService { @GET("suggest/{keyword}") Call<List<String>> listSuggestions( @Path("keyword") String keyword); } @OnTextChanged(R.id.keyword) public void keywordChanged(String keyword) { Call<List<String>> suggestions = service.listSuggestions(keyword); suggestions.enqueue(new Callback<List<String>>() { @Override public void onResponse(Call<List<String>> call, Response<List<String>> response) { List<String> texts = response.body(); for (String t : texts) { adapter.add(t); } } @Override public void onFailure(Call<List<String>> call, Throwable t) { } }); } public interface MyRestService { @GET("suggest/{keyword}") Observable<List<String>> listSuggestions( @Path("keyword") String keyword); } RxTextView.textChanges(R.id.keyword) .map(CharSequence::toString) .flatMap(text -> service.listSuggestions(text)) .flatMapIterable(texts -> texts) .subscribe(adapter::add);
  9. ޥо য࢝೧ ઑӘ ؊ ࢎਊ੗ীѱ ஘ച੸੉ݶ જѷয ߡ੹ ৢۄоח ܻࣗо

    ٜ۰ ఃਕ٘ ੑ۱ೞח द੼ী ৵ ੉੹ ఃਕ٘ী ؀ೠ ܻझ౟ܳ ਫ਼Ӭ ࠁৈ઱૑? ੉ѱ غח Ѥо? উ غח Ѥо? ী۞ աחؘ ઁо ަ ੜޅೠѤоਃ? ডр ખ וܽ ו՝ੋؘ ؊ ࡅܰѱ উغաਃ?
  10. TextViewী OnTextChangedListenerܳ ׳Ҋ, ੑ۱ػ ఫझ౟۽ HTTP ௿ۄ੉঱౟ܳ ా೧ Suggestion ఫझ౟

    ܻझ౟ܳ ߉ইৡ׮. ߉ইৡ ܻझ౟ܳ ߃ী ੓ח RecyclerView੄ Adapterী ߈৔ೠ׮. RecyclerView੄ Adapter੄ ؘ੉ఠܳ ࠺ਕળ׮. ՘աݶ ProgressBarܳ ऀӟ׮. ProgressBarܳ ࠁৈળ׮. ೞ׮о ޥо ޙઁо ࢤӝݶ ࢎਊ੗ೠప ஘੺ೞѱ ঌ۰ળ׮. SnackBarܳ ࠁৈ઱੗. TextViewী ఫझ౟о হѢա झಕ੉झ݅ ੓ਸ ٸח ֎౟ਕ௼ө૑ о૑ ݈੗.
  11. without Rx with Rx TextViewী OnTextChangedListenerܳ ׳Ҋ, द੘ೞӝ ੹ী Adapterী

    ؘ੉ఠܳ ૑਍׮. ੑ۱ػ ఫझ౟۽ HTTP ௿ۄ੉঱౟ܳ ా೧ Suggestion ఫझ౟ ܻझ౟ܳ ߉ইৡ׮. ߉ইৡ ܻझ౟ܳ ߃ী ੓ח RecyclerView੄ Adapterী ߈৔ೠ׮. @OnTextChanged(R.id.keyword) public void keywordChanged(String keyword) { adapter.clear(); Call<List<String>> suggestions = service.listSuggestions(keyword); suggestions.enqueue(new Callback<List<String>>() { @Override public void onResponse(Call<List<String>> call, Response<List<String>> response) { List<String> texts = response.body(); for (String t : texts) { adapter.add(t); } } @Override public void onFailure(Call<List<String>> call, Throwable t) { } }); } RxTextView.textChanges(R.id.keyword) .map(CharSequence::toString) .flatMap(text -> service.listSuggestions(text)) .flatMapIterable(texts -> texts) .subscribe(adapter::add, e -> {}, () -> {}, __ -> adapter.clear());
  12. without Rx with Rx TextViewী OnTextChangedListenerܳ ׳Ҋ, द੘ೞӝ ੹ী Adapterী

    ؘ੉ఠܳ ૑਋Ҋ ೐۽Ӓۨझ߄ܳ ࠁৈળ׮. ੑ۱ػ ఫझ౟۽ HTTP ௿ۄ੉঱౟ܳ ా೧ Suggestion ఫझ౟ ܻझ౟ܳ ߉ইৡ׮. ߉ইৡ ܻझ౟ܳ ߃ী ੓ח RecyclerView੄ Adapterী ߈৔ೠ׮. ՘աݶ ೐۽Ӓۨझ߄ܳ ऀӟ׮. @OnTextChanged(R.id.keyword) public void keywordChanged(String keyword) { adapter.clear(); progressBar.setVisibility(VISIBLE); Call<List<String>> suggestions = service.listSuggestions(keyword); suggestions.enqueue(new Callback<List<String>>() { @Override public void onResponse(Call<List<String>> call, Response<List<String>> response) { List<String> texts = response.body(); for (String t : texts) { adapter.add(t); } progressBar.setVisibility(GONE); } @Override public void onFailure(Call<List<String>> call, Throwable t) { } }); } RxTextView.textChanges(R.id.keyword) .map(CharSequence::toString) .flatMap(text -> service.listSuggestions(text)) .flatMapIterable(texts -> texts) .subscribe(adapter::add, e -> {}, () -> progressBar.setVisibility(GONE), __ -> { adapter.clear(); progressBar.setVisibility(VISIBLE); });
  13. without Rx with Rx TextViewী OnTextChangedListenerܳ ׳Ҋ, द੘ೞӝ ੹ী Adapterী

    ؘ੉ఠܳ ૑਋Ҋ ೐۽Ӓۨझ߄ܳ ࠁৈળ׮. ੑ۱ػ ఫझ౟۽ HTTP ௿ۄ੉঱౟ܳ ా೧ Suggestion ఫझ౟ ܻझ౟ܳ ߉ইৡ׮. ߉ইৡ ܻझ౟ܳ ߃ী ੓ח RecyclerView੄ Adapterী ߈৔ೠ׮. ՘աݶ ೐۽Ӓۨझ߄ܳ ऀӟ׮. ী۞о ߊࢤೞݶ झճ߄ܳ ࠁৈળ׮. @OnTextChanged(R.id.keyword) public void keywordChanged(String keyword) { adapter.clear(); progressBar.setVisibility(VISIBLE); Call<List<String>> suggestions = service.listSuggestions(keyword); suggestions.enqueue(new Callback<List<String>>() { @Override public void onResponse(Call<List<String>> call, Response<List<String>> response) { List<String> texts = response.body(); for (String t : texts) { adapter.add(t); } progressBar.setVisibility(GONE); } @Override public void onFailure(Call<List<String>> call, Throwable t) { SnackBar.make(view, "Something wrong!", LENGTH_LONG).show(); Log.d(TAG, "Error on keywordChanged.", t); } }); } RxTextView.textChanges(R.id.keyword) .map(CharSequence::toString) .flatMap(text -> service.listSuggestions(text)) .flatMapIterable(texts -> texts) .subscribe(adapter::add, e -> { SnackBar.make(view, "Something wrong!", LENGTH_LONG).show(); Log.d(TAG, "Error on keywordChanged.", e); }, () -> progressBar.setVisibility(GONE), __ -> { adapter.clear(); progressBar.setVisibility(VISIBLE); });
  14. without Rx with Rx TextViewী OnTextChangedListenerܳ ׳Ҋ, ੑ۱ػ ఃਕ٘о ਬബೠ૑

    ୓௼ೠ׮. द੘ೞӝ ੹ী Adapterী ؘ੉ఠܳ ૑਋Ҋ ೐۽Ӓۨझ߄ܳ ࠁৈળ׮. ੑ۱ػ ఫझ౟۽ HTTP ௿ۄ੉঱౟ܳ ా೧ Suggestion ఫझ౟ ܻझ౟ܳ ߉ইৡ׮. ߉ইৡ ܻझ౟ܳ ߃ী ੓ח RecyclerView੄ Adapterী ߈৔ೠ׮. ՘աݶ ೐۽Ӓۨझ߄ܳ ऀӟ׮. ী۞о ߊࢤೞݶ झճ߄ܳ ࠁৈળ׮. @OnTextChanged(R.id.keyword) public void keywordChanged(String keyword) { if(““.equals(keyword.trim()) return; adapter.clear(); progressBar.setVisibility(VISIBLE); Call<List<String>> suggestions = service.listSuggestions(keyword); suggestions.enqueue(new Callback<List<String>>() { @Override public void onResponse(Call<List<String>> call, Response<List<String>> response) { List<String> texts = response.body(); for (String t : texts) { adapter.add(t); } progressBar.setVisibility(GONE); } @Override public void onFailure(Call<List<String>> call, Throwable t) { SnackBar.make(view, "Something wrong!", LENGTH_LONG).show(); Log.d(TAG, "Error on keywordChanged.", t); } }); } RxTextView.textChanges(R.id.keyword) .map(CharSequence::toString) .map(String::trim) .filter(t -> !"".equals(t)) .flatMap(text -> service.listSuggestions(text)) .flatMapIterable(texts -> texts) .subscribe(adapter::add, e -> { SnackBar.make(view, "Something wrong!", LENGTH_LONG).show(); Log.d(TAG, "Error on keywordChanged.", e); }, () -> progressBar.setVisibility(GONE), __ -> { adapter.clear(); progressBar.setVisibility(VISIBLE); });
  15. Congratulations! Level 2 • Hot/Cold Observable • more APIs of

    RxJava • Test for each onNext Next Quests http://wowguidessource.com/5-wow-leveling-tips-to-acquire-world-of-warcraft-level-85-fast.php • Future<T>, Single<T>
  16. Reference • ReactiveX ҕध ࢎ੉౟ http://reactivex.io/ • GDG 2014 -

    RxJavaܳ ഝਊೠ Functional Reactive Programming https://www.slideshare.net/waynejo/gdg-41029986 • ਢ ೐ۿ౟ূ٘ ѐߊ੗੄ ঩Ҋ և਷ Rx ੉ঠӝ https:// www.slideshare.net/jeokrang/rx-70197043 • زӝ৬ ࠺زӝ, ࠶۽ఊҗ ֤࠶۽ఊ http://nsinc.tistory.com/108 • http://nsinc.tistory.com/108