SlideShare une entreprise Scribd logo
1  sur  20
Télécharger pour lire hors ligne
RxJava and Using RxJava
in MVP
TOI DUONG VAN (STEVE)
ANDROID DEVELOPER – INNOVATUBE SOLUTION
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
Definition
// 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
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);
});
}
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);
}
});
}
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);
});
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) {
}
});
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);
}
});
}
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);
}
});
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();
Conclusion
Tech Talk #4 : RxJava and Using RxJava in MVP - Dương Văn Tới
The old time
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();
}
}
Model View Presenter
Tech Talk #4 : RxJava and Using RxJava in MVP - Dương Văn Tới
Source code
 https://github.com/toidv/MVP-Clean-Architecture
Q & A
THANK YOU!

Contenu connexe

Tendances

Przywitaj się z reactive extensions
Przywitaj się z reactive extensionsPrzywitaj się z reactive extensions
Przywitaj się z reactive extensionsMarcin Juraszek
 
Angular and The Case for RxJS
Angular and The Case for RxJSAngular and The Case for RxJS
Angular and The Case for RxJSSandi Barr
 
Retrofit
RetrofitRetrofit
Retrofitbresiu
 
Deep Dumpster Diving
Deep Dumpster DivingDeep Dumpster Diving
Deep Dumpster DivingRonnBlack
 
RxJS - The Reactive extensions for JavaScript
RxJS - The Reactive extensions for JavaScriptRxJS - The Reactive extensions for JavaScript
RxJS - The Reactive extensions for JavaScriptViliam Elischer
 
RxJS101 - What you need to know to get started with RxJS tomorrow
RxJS101 - What you need to know to get started with RxJS tomorrowRxJS101 - What you need to know to get started with RxJS tomorrow
RxJS101 - What you need to know to get started with RxJS tomorrowViliam Elischer
 
My Gentle Introduction to RxJS
My Gentle Introduction to RxJSMy Gentle Introduction to RxJS
My Gentle Introduction to RxJSMattia Occhiuto
 
RxJS Evolved
RxJS EvolvedRxJS Evolved
RxJS Evolvedtrxcllnt
 
RxJS Operators - Real World Use Cases (FULL VERSION)
RxJS Operators - Real World Use Cases (FULL VERSION)RxJS Operators - Real World Use Cases (FULL VERSION)
RxJS Operators - Real World Use Cases (FULL VERSION)Tracy Lee
 
Data binding in AngularJS, from model to view
Data binding in AngularJS, from model to viewData binding in AngularJS, from model to view
Data binding in AngularJS, from model to viewThomas Roch
 
Educational slides by venay magen
Educational slides by venay magenEducational slides by venay magen
Educational slides by venay magenvenaymagen19
 
Async JavaScript in ES7
Async JavaScript in ES7Async JavaScript in ES7
Async JavaScript in ES7Mike North
 
Microsoft Ado
Microsoft AdoMicrosoft Ado
Microsoft Adooswchavez
 
Java practice programs for beginners
Java practice programs for beginnersJava practice programs for beginners
Java practice programs for beginnersishan0019
 
RxJava applied [JavaDay Kyiv 2016]
RxJava applied [JavaDay Kyiv 2016]RxJava applied [JavaDay Kyiv 2016]
RxJava applied [JavaDay Kyiv 2016]Igor Lozynskyi
 
Reactive X introduction part1
Reactive X introduction part1Reactive X introduction part1
Reactive X introduction part1Jieyi Wu
 

Tendances (20)

Przywitaj się z reactive extensions
Przywitaj się z reactive extensionsPrzywitaj się z reactive extensions
Przywitaj się z reactive extensions
 
Angular and The Case for RxJS
Angular and The Case for RxJSAngular and The Case for RxJS
Angular and The Case for RxJS
 
Retrofit
RetrofitRetrofit
Retrofit
 
Deep Dumpster Diving
Deep Dumpster DivingDeep Dumpster Diving
Deep Dumpster Diving
 
RxJS - The Reactive extensions for JavaScript
RxJS - The Reactive extensions for JavaScriptRxJS - The Reactive extensions for JavaScript
RxJS - The Reactive extensions for JavaScript
 
RxJS101 - What you need to know to get started with RxJS tomorrow
RxJS101 - What you need to know to get started with RxJS tomorrowRxJS101 - What you need to know to get started with RxJS tomorrow
RxJS101 - What you need to know to get started with RxJS tomorrow
 
Rx – reactive extensions
Rx – reactive extensionsRx – reactive extensions
Rx – reactive extensions
 
My Gentle Introduction to RxJS
My Gentle Introduction to RxJSMy Gentle Introduction to RxJS
My Gentle Introduction to RxJS
 
RxJS Evolved
RxJS EvolvedRxJS Evolved
RxJS Evolved
 
RxJS Operators - Real World Use Cases (FULL VERSION)
RxJS Operators - Real World Use Cases (FULL VERSION)RxJS Operators - Real World Use Cases (FULL VERSION)
RxJS Operators - Real World Use Cases (FULL VERSION)
 
XTW_Import
XTW_ImportXTW_Import
XTW_Import
 
Data binding in AngularJS, from model to view
Data binding in AngularJS, from model to viewData binding in AngularJS, from model to view
Data binding in AngularJS, from model to view
 
Oop assignment 02
Oop assignment 02Oop assignment 02
Oop assignment 02
 
Educational slides by venay magen
Educational slides by venay magenEducational slides by venay magen
Educational slides by venay magen
 
Async JavaScript in ES7
Async JavaScript in ES7Async JavaScript in ES7
Async JavaScript in ES7
 
Microsoft Ado
Microsoft AdoMicrosoft Ado
Microsoft Ado
 
Java practice programs for beginners
Java practice programs for beginnersJava practice programs for beginners
Java practice programs for beginners
 
RxJava applied [JavaDay Kyiv 2016]
RxJava applied [JavaDay Kyiv 2016]RxJava applied [JavaDay Kyiv 2016]
RxJava applied [JavaDay Kyiv 2016]
 
Reactive X introduction part1
Reactive X introduction part1Reactive X introduction part1
Reactive X introduction part1
 
From * to Symfony2
From * to Symfony2From * to Symfony2
From * to Symfony2
 

En vedette

Infinum Android Talks #12 - MVP design pattern for Android Apps
Infinum Android Talks #12 - MVP design pattern for Android AppsInfinum Android Talks #12 - MVP design pattern for Android Apps
Infinum Android Talks #12 - MVP design pattern for Android AppsInfinum
 
Android Architecture MVP Pattern
Android Architecture MVP Pattern Android Architecture MVP Pattern
Android Architecture MVP Pattern Jeff Potter
 
Practical RxJava for Android
Practical RxJava for AndroidPractical RxJava for Android
Practical RxJava for AndroidTomáš Kypta
 
Acrhitecture deisign pattern_MVC_MVP_MVVM
Acrhitecture deisign pattern_MVC_MVP_MVVMAcrhitecture deisign pattern_MVC_MVP_MVVM
Acrhitecture deisign pattern_MVC_MVP_MVVMDong-Ho Lee
 
Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...
Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...
Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...Svetlin Nakov
 

En vedette (7)

Infinum Android Talks #12 - MVP design pattern for Android Apps
Infinum Android Talks #12 - MVP design pattern for Android AppsInfinum Android Talks #12 - MVP design pattern for Android Apps
Infinum Android Talks #12 - MVP design pattern for Android Apps
 
Android Architecture MVP Pattern
Android Architecture MVP Pattern Android Architecture MVP Pattern
Android Architecture MVP Pattern
 
MVP Clean Architecture
MVP Clean  Architecture MVP Clean  Architecture
MVP Clean Architecture
 
Practical RxJava for Android
Practical RxJava for AndroidPractical RxJava for Android
Practical RxJava for Android
 
Acrhitecture deisign pattern_MVC_MVP_MVVM
Acrhitecture deisign pattern_MVC_MVP_MVVMAcrhitecture deisign pattern_MVC_MVP_MVVM
Acrhitecture deisign pattern_MVC_MVP_MVVM
 
Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...
Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...
Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...
 
Slide Presentation of MVP Pattern Concept
Slide Presentation of MVP Pattern ConceptSlide Presentation of MVP Pattern Concept
Slide Presentation of MVP Pattern Concept
 

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

Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on AndroidTomáš Kypta
 
GKAC 2015 Apr. - RxAndroid
GKAC 2015 Apr. - RxAndroidGKAC 2015 Apr. - RxAndroid
GKAC 2015 Apr. - RxAndroidGDG Korea
 
Rxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJavaRxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJavaKros Huang
 
Building Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaBuilding Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaRick Warren
 
Functional UIs with Java 8 and Vaadin JavaOne2014
Functional UIs with Java 8 and Vaadin JavaOne2014Functional UIs with Java 8 and Vaadin JavaOne2014
Functional UIs with Java 8 and Vaadin JavaOne2014hezamu
 
Painless Persistence with Realm
Painless Persistence with RealmPainless Persistence with Realm
Painless Persistence with RealmChristian Melchior
 
Reactive Programming on Android
Reactive Programming on AndroidReactive Programming on Android
Reactive Programming on AndroidGuilherme Branco
 
RxJava for Android - GDG DevFest Ukraine 2015
RxJava for Android - GDG DevFest Ukraine 2015RxJava for Android - GDG DevFest Ukraine 2015
RxJava for Android - GDG DevFest Ukraine 2015Constantine Mars
 
"Kotlin и rx в android" Дмитрий Воронин (Avito)
"Kotlin и rx в android" Дмитрий Воронин  (Avito)"Kotlin и rx в android" Дмитрий Воронин  (Avito)
"Kotlin и rx в android" Дмитрий Воронин (Avito)AvitoTech
 
RxJava и Android. Плюсы, минусы, подводные камни
RxJava и Android. Плюсы, минусы, подводные камниRxJava и Android. Плюсы, минусы, подводные камни
RxJava и Android. Плюсы, минусы, подводные камниStfalcon Meetups
 
Modern Android app library stack
Modern Android app library stackModern Android app library stack
Modern Android app library stackTomáš Kypta
 
10. Kapusta, Stofanak - eGlu
10. Kapusta, Stofanak - eGlu10. Kapusta, Stofanak - eGlu
10. Kapusta, Stofanak - eGluMobCon
 
Akka Cluster in Java - JCConf 2015
Akka Cluster in Java - JCConf 2015Akka Cluster in Java - JCConf 2015
Akka Cluster in Java - JCConf 2015Jiayun Zhou
 
Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015Leonardo Borges
 
响应式编程及框架
响应式编程及框架响应式编程及框架
响应式编程及框架jeffz
 

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

Rx java in action
Rx java in actionRx java in action
Rx java in action
 
Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on Android
 
GKAC 2015 Apr. - RxAndroid
GKAC 2015 Apr. - RxAndroidGKAC 2015 Apr. - RxAndroid
GKAC 2015 Apr. - RxAndroid
 
Rxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJavaRxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJava
 
Saving lives with rx java
Saving lives with rx javaSaving lives with rx java
Saving lives with rx java
 
Building Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaBuilding Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJava
 
Functional UIs with Java 8 and Vaadin JavaOne2014
Functional UIs with Java 8 and Vaadin JavaOne2014Functional UIs with Java 8 and Vaadin JavaOne2014
Functional UIs with Java 8 and Vaadin JavaOne2014
 
Rx workshop
Rx workshopRx workshop
Rx workshop
 
Painless Persistence with Realm
Painless Persistence with RealmPainless Persistence with Realm
Painless Persistence with Realm
 
Reactive Programming on Android
Reactive Programming on AndroidReactive Programming on Android
Reactive Programming on Android
 
RxJava for Android - GDG DevFest Ukraine 2015
RxJava for Android - GDG DevFest Ukraine 2015RxJava for Android - GDG DevFest Ukraine 2015
RxJava for Android - GDG DevFest Ukraine 2015
 
"Kotlin и rx в android" Дмитрий Воронин (Avito)
"Kotlin и rx в android" Дмитрий Воронин  (Avito)"Kotlin и rx в android" Дмитрий Воронин  (Avito)
"Kotlin и rx в android" Дмитрий Воронин (Avito)
 
RxJava и Android. Плюсы, минусы, подводные камни
RxJava и Android. Плюсы, минусы, подводные камниRxJava и Android. Плюсы, минусы, подводные камни
RxJava и Android. Плюсы, минусы, подводные камни
 
Reactive Java (GeeCON 2014)
Reactive Java (GeeCON 2014)Reactive Java (GeeCON 2014)
Reactive Java (GeeCON 2014)
 
Modern Android app library stack
Modern Android app library stackModern Android app library stack
Modern Android app library stack
 
10. Kapusta, Stofanak - eGlu
10. Kapusta, Stofanak - eGlu10. Kapusta, Stofanak - eGlu
10. Kapusta, Stofanak - eGlu
 
Akka Cluster in Java - JCConf 2015
Akka Cluster in Java - JCConf 2015Akka Cluster in Java - JCConf 2015
Akka Cluster in Java - JCConf 2015
 
Kpi driven-java-development
Kpi driven-java-developmentKpi driven-java-development
Kpi driven-java-development
 
Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015
 
响应式编程及框架
响应式编程及框架响应式编程及框架
响应式编程及框架
 

Plus de Nexus FrontierTech

[Executive Lounge Talk] Digital Transformation Journey
[Executive Lounge Talk] Digital Transformation Journey[Executive Lounge Talk] Digital Transformation Journey
[Executive Lounge Talk] Digital Transformation JourneyNexus FrontierTech
 
[AI series Talk #2] From PoC to Production - A Case Study
[AI series Talk #2] From PoC to Production - A Case Study[AI series Talk #2] From PoC to Production - A Case Study
[AI series Talk #2] From PoC to Production - A Case StudyNexus FrontierTech
 
[AI Series Talk #2] Moving AI from PoC Stage to Production
[AI Series Talk #2] Moving AI from PoC Stage to Production[AI Series Talk #2] Moving AI from PoC Stage to Production
[AI Series Talk #2] Moving AI from PoC Stage to ProductionNexus FrontierTech
 
[VFS 2019] Introduction to GANs - Pixta Vietnam
[VFS 2019] Introduction to GANs - Pixta Vietnam[VFS 2019] Introduction to GANs - Pixta Vietnam
[VFS 2019] Introduction to GANs - Pixta VietnamNexus FrontierTech
 
[VFS 2019] Enabling Young Generation for Future - AWS Vietnam User Group
[VFS 2019] Enabling Young Generation for Future - AWS Vietnam User Group[VFS 2019] Enabling Young Generation for Future - AWS Vietnam User Group
[VFS 2019] Enabling Young Generation for Future - AWS Vietnam User GroupNexus FrontierTech
 
[VFS 2019] Building chatbot with RASA
[VFS 2019] Building chatbot with RASA[VFS 2019] Building chatbot with RASA
[VFS 2019] Building chatbot with RASANexus FrontierTech
 
[VFS 2019] Vietnamese Speech-to-Text: Applications and Product
[VFS 2019] Vietnamese Speech-to-Text: Applications and Product[VFS 2019] Vietnamese Speech-to-Text: Applications and Product
[VFS 2019] Vietnamese Speech-to-Text: Applications and ProductNexus FrontierTech
 
[VFS 2019] How AI Will Innovate Recruitment
[VFS 2019] How AI Will Innovate Recruitment[VFS 2019] How AI Will Innovate Recruitment
[VFS 2019] How AI Will Innovate RecruitmentNexus FrontierTech
 
[VFS 2019] Preventive Approach to Designing and Selling Healthy AI System
[VFS 2019] Preventive Approach to Designing and Selling Healthy AI System [VFS 2019] Preventive Approach to Designing and Selling Healthy AI System
[VFS 2019] Preventive Approach to Designing and Selling Healthy AI System Nexus FrontierTech
 
[VFS 2019] Phương pháp phát hiện bất thường bằng học máy
[VFS 2019] Phương pháp phát hiện bất thường bằng học máy[VFS 2019] Phương pháp phát hiện bất thường bằng học máy
[VFS 2019] Phương pháp phát hiện bất thường bằng học máyNexus FrontierTech
 
[VFS 2019] OCR Techniques for Digital Transformation Evolution
[VFS 2019] OCR Techniques for Digital Transformation Evolution[VFS 2019] OCR Techniques for Digital Transformation Evolution
[VFS 2019] OCR Techniques for Digital Transformation EvolutionNexus FrontierTech
 
[VFS 2019] Human Activity Recognition Approaches
[VFS 2019] Human Activity Recognition Approaches [VFS 2019] Human Activity Recognition Approaches
[VFS 2019] Human Activity Recognition Approaches Nexus FrontierTech
 
[VFS 2019] Datamart Introduction (brief)
[VFS 2019] Datamart Introduction (brief)[VFS 2019] Datamart Introduction (brief)
[VFS 2019] Datamart Introduction (brief)Nexus FrontierTech
 
[VFS 2019] Data Strategy for Vietnamese Businesses to Levarage AI
[VFS 2019] Data Strategy for Vietnamese Businesses to Levarage AI[VFS 2019] Data Strategy for Vietnamese Businesses to Levarage AI
[VFS 2019] Data Strategy for Vietnamese Businesses to Levarage AINexus FrontierTech
 
[VFS 2019] Digital Solution for Enterprises: 24/7 A.I English Speaking Coach
[VFS 2019] Digital Solution for Enterprises: 24/7 A.I English Speaking Coach[VFS 2019] Digital Solution for Enterprises: 24/7 A.I English Speaking Coach
[VFS 2019] Digital Solution for Enterprises: 24/7 A.I English Speaking CoachNexus FrontierTech
 
[VFS 2019] Project Management for AI-based Product - A Better Approach
[VFS 2019] Project Management for AI-based Product - A Better Approach[VFS 2019] Project Management for AI-based Product - A Better Approach
[VFS 2019] Project Management for AI-based Product - A Better ApproachNexus FrontierTech
 
[VFS 2019] AI Ecosystem transition from zero to hero - case study by rubikAI
[VFS 2019] AI Ecosystem transition from zero to hero - case study by rubikAI[VFS 2019] AI Ecosystem transition from zero to hero - case study by rubikAI
[VFS 2019] AI Ecosystem transition from zero to hero - case study by rubikAINexus FrontierTech
 

Plus de Nexus FrontierTech (20)

[Executive Lounge Talk] Digital Transformation Journey
[Executive Lounge Talk] Digital Transformation Journey[Executive Lounge Talk] Digital Transformation Journey
[Executive Lounge Talk] Digital Transformation Journey
 
[AI series Talk #2] From PoC to Production - A Case Study
[AI series Talk #2] From PoC to Production - A Case Study[AI series Talk #2] From PoC to Production - A Case Study
[AI series Talk #2] From PoC to Production - A Case Study
 
[AI Series Talk #2] Moving AI from PoC Stage to Production
[AI Series Talk #2] Moving AI from PoC Stage to Production[AI Series Talk #2] Moving AI from PoC Stage to Production
[AI Series Talk #2] Moving AI from PoC Stage to Production
 
[VFS 2019] Introduction to GANs - Pixta Vietnam
[VFS 2019] Introduction to GANs - Pixta Vietnam[VFS 2019] Introduction to GANs - Pixta Vietnam
[VFS 2019] Introduction to GANs - Pixta Vietnam
 
[VFS 2019] Enabling Young Generation for Future - AWS Vietnam User Group
[VFS 2019] Enabling Young Generation for Future - AWS Vietnam User Group[VFS 2019] Enabling Young Generation for Future - AWS Vietnam User Group
[VFS 2019] Enabling Young Generation for Future - AWS Vietnam User Group
 
[VFS 2019] Building chatbot with RASA
[VFS 2019] Building chatbot with RASA[VFS 2019] Building chatbot with RASA
[VFS 2019] Building chatbot with RASA
 
[VFS 2019] Vietnamese Speech-to-Text: Applications and Product
[VFS 2019] Vietnamese Speech-to-Text: Applications and Product[VFS 2019] Vietnamese Speech-to-Text: Applications and Product
[VFS 2019] Vietnamese Speech-to-Text: Applications and Product
 
[VFS 2019] How AI Will Innovate Recruitment
[VFS 2019] How AI Will Innovate Recruitment[VFS 2019] How AI Will Innovate Recruitment
[VFS 2019] How AI Will Innovate Recruitment
 
[VFS 2019] AI in Finance
[VFS 2019] AI in Finance[VFS 2019] AI in Finance
[VFS 2019] AI in Finance
 
[VFS 2019] Preventive Approach to Designing and Selling Healthy AI System
[VFS 2019] Preventive Approach to Designing and Selling Healthy AI System [VFS 2019] Preventive Approach to Designing and Selling Healthy AI System
[VFS 2019] Preventive Approach to Designing and Selling Healthy AI System
 
[VFS 2019] Phương pháp phát hiện bất thường bằng học máy
[VFS 2019] Phương pháp phát hiện bất thường bằng học máy[VFS 2019] Phương pháp phát hiện bất thường bằng học máy
[VFS 2019] Phương pháp phát hiện bất thường bằng học máy
 
[VFS 2019] OCR Techniques for Digital Transformation Evolution
[VFS 2019] OCR Techniques for Digital Transformation Evolution[VFS 2019] OCR Techniques for Digital Transformation Evolution
[VFS 2019] OCR Techniques for Digital Transformation Evolution
 
[VFS 2019] Human Activity Recognition Approaches
[VFS 2019] Human Activity Recognition Approaches [VFS 2019] Human Activity Recognition Approaches
[VFS 2019] Human Activity Recognition Approaches
 
[VFS 2019] Aimesoft Solutions
[VFS 2019] Aimesoft Solutions[VFS 2019] Aimesoft Solutions
[VFS 2019] Aimesoft Solutions
 
[VFS 2019] Datamart Introduction (brief)
[VFS 2019] Datamart Introduction (brief)[VFS 2019] Datamart Introduction (brief)
[VFS 2019] Datamart Introduction (brief)
 
[VFS 2019] Data Strategy for Vietnamese Businesses to Levarage AI
[VFS 2019] Data Strategy for Vietnamese Businesses to Levarage AI[VFS 2019] Data Strategy for Vietnamese Businesses to Levarage AI
[VFS 2019] Data Strategy for Vietnamese Businesses to Levarage AI
 
[VFS 2019] AI for Banks
[VFS 2019] AI for Banks[VFS 2019] AI for Banks
[VFS 2019] AI for Banks
 
[VFS 2019] Digital Solution for Enterprises: 24/7 A.I English Speaking Coach
[VFS 2019] Digital Solution for Enterprises: 24/7 A.I English Speaking Coach[VFS 2019] Digital Solution for Enterprises: 24/7 A.I English Speaking Coach
[VFS 2019] Digital Solution for Enterprises: 24/7 A.I English Speaking Coach
 
[VFS 2019] Project Management for AI-based Product - A Better Approach
[VFS 2019] Project Management for AI-based Product - A Better Approach[VFS 2019] Project Management for AI-based Product - A Better Approach
[VFS 2019] Project Management for AI-based Product - A Better Approach
 
[VFS 2019] AI Ecosystem transition from zero to hero - case study by rubikAI
[VFS 2019] AI Ecosystem transition from zero to hero - case study by rubikAI[VFS 2019] AI Ecosystem transition from zero to hero - case study by rubikAI
[VFS 2019] AI Ecosystem transition from zero to hero - case study by rubikAI
 

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

  • 1. RxJava and Using RxJava in MVP TOI DUONG VAN (STEVE) ANDROID DEVELOPER – INNOVATUBE SOLUTION
  • 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
  • 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. 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. 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. 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. 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. 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. 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. 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();
  • 15. 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(); } }
  • 19. Q & A