Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Tech Talk #4 : RxJava and Using RxJava in MVP - Dương Văn Tới

RxJava and Using RxJava in MVP

  • Be the first to comment

Tech Talk #4 : RxJava and Using RxJava in MVP - Dương Văn Tới

  1. 1. RxJava and Using RxJava in MVP TOI DUONG VAN (STEVE) ANDROID DEVELOPER – INNOVATUBE SOLUTION
  2. 2. Definition  RxJava  A library for composing asynchronous and event-base programming using observable sequences for the JavaVM  A combination from the Observer pattern, the Iterator pattern, and functional programming  A lot of functions to create, combine and filter any of those streams
  3. 3. Definition
  4. 4. // Convert any object into an Observable that emits that object Observable.just("Hello world!"); // Converts an Array into an Observable that emits the items in the Array. Integer[] numbers = {0, 1, 2, 3, 4, 5}; Observable.from(numbers); // From Retrofit public Observable<SearchResult> searchRepo(String name, String sort) { return mGithubService.searchRepo(name, sort); } @GET("search/users") Observable<SearchResult> searchRepo(@Query("q") String name, @Query("sort") String sort); // Custom from anything Observable<String> myObservable = Observable.create(new Observable.OnSubscribe<String>() { @Override public void call(Subscriber<? super String> subscriber) { try { String s = doSomething(); subscriber.onNext(s); subscriber.onCompleted(); } catch (Exception e) { subscriber.onError(e); } } }); Observable
  5. 5. Scheduler private void heavyComputationFunction() { Observable.create(new Observable.OnSubscribe<String>() { @Override public void call(Subscriber<? super String> subscriber) { for (int i = 0; i < 1000000; i++) { subscriber.onNext(i + ""); } subscriber.onCompleted(); } }).subscribeOn(Schedulers.computation()) // Scheduler intended for computational work .subscribe(s -> { Log.d(TAG, s); }); }
  6. 6. Scheduler private void networkCallGetRepo(String search, String sort) { mSbuscription = mDataManager.searchRepo(search, sort) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber<Item>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { e.printStackTrace(); } @Override public void onNext(Item item) { getMvpView().addUser(item); } }); }
  7. 7. Operators // Log the length of input string Observable.just("Hello world") .map(s -> s.length()) .map(integer -> integer.toString()) .subscribe(string -> { Log.d(TAG, string); }); // Log the even number Integer[] integers = {1, 2, 3, 4, 5, 6, 7, 8}; Observable.from(integers) .filter(integer -> (integer % 2 == 0)) .map(integer -> integer.toString()) .subscribe(s -> { Log.d(TAG, s); });
  8. 8. Scheduler Observable.create(new Observable.OnSubscribe<String>() { @Override public void call(Subscriber<? super String> subscriber) { getActivityComponent().inject(ReposActivity.this); mReposPresenter.attachView(ReposActivity.this); subscriber.onCompleted(); } }) .subscribeOn(Schedulers.io()) .observeOn(Schedulers.io()) .subscribe(new Observer<String>() { @Override public void onCompleted() { getRepos(mRepoUrl); } @Override public void onError(Throwable e) { e.printStackTrace(); } @Override public void onNext(String s) { } });
  9. 9. Scheduler public void getRepos(String url) { mSbuscription = mDataManager.getRepos(url) .filter(repos -> repos != null) .concatMap(repos -> Observable.from(repos)) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber<Repo>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { e.printStackTrace(); } @Override public void onNext(Repo repo) { getMvpView().addRepo(repo); } }); }
  10. 10. Subscriber Observable.just("Action1") .subscribe(new Action1<String>() { @Override public void call(String s) { Log.d(TAG, s); } }); Observable.just("Subscriber") .subscribe(new Subscriber<String>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(String s) { Log.d(TAG, s); } }); Observable.just("Observer") .subscribe(new Observer<String>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { e.printStackTrace(); } @Override public void onNext(String s) { Log.d(TAG, s); } });
  11. 11. Subscribers ConnectableObservable<Integer> connectableObservable = Observable.range(0, 5).publish(); connectableObservable.filter(integer -> integer % 2 == 0) .map(integer -> integer.toString()) .subscribe(string -> { Log.d(TAG, "even " +string); }); connectableObservable.filter((integer -> (integer % 2 != 0))) .map(integer -> integer.toString()) .subscribe(new Action1<String>() { @Override public void call(String s) { Log.d(TAG, "odd "+s); } }); connectableObservable.connect();
  12. 12. Conclusion
  13. 13. The old time
  14. 14. Conclusion class DownloadTask extends AsyncTask<String, Void, File> { protected File doInBackground(String... args) { final String url = args[0]; try { byte[] fileContent = downloadFile(url); File file = writeToFile(fileContent); return file; } catch (Exception e) { // How to show alert error to user ??? } } protected void onPostExecute(File file) { Context context = getContext(); // Memory leak ??? Toast.makeText(context, "Downloaded: " + file.getAbsolutePath(), Toast.LENGTH_SHORT) .show(); } }
  15. 15. Model View Presenter
  16. 16. Source code  https://github.com/toidv/MVP-Clean-Architecture
  17. 17. Q & A
  18. 18. THANK YOU!

×