Effective code with QuickBlox & RxAndroid
Here are some tips about using RX programming with QB SDK Android. Start learning RX world with QuickBlox!
Reactive programming is a programming paradigm oriented around data flows and the propagation of change. RxJava is reactive implementation successfully used on Android. The best place to start immerse into the reactive world is Android. Add here QuickBlox & RxAndroid — and you get the most effective reactive combination!
If you already use QuickBlox Android SDK, you know how easy and exciting can be creating a wide range of communication features included (such as one-to-one messaging, group chat messaging, file transfer, push notifications, audio/video calls), if you don’t — before continuing you should take a look.
Briefing: In order to login to the chat and start communication, you must sign in to QuickBlox API REST and then log in to QuickBlox Chat with user.
QBUser - model of the QuickBlox user representationChatDialog - model describes a chat dialog entity between users (1-1 chat or group chat)Performer - an invocation of a QB API REST request that sends a request to a webserver and returns a responseRxJavaPerformProcessor - model which uses RxJava for creating observables
So, let’s start using RX with performing QBUsers.signIn async request:
Sure, when using a lambda expression, there will be less code and it will become even more clear in a functional manner.
Some words about SubscribeOn & ObserveOn:
The SubscribeOn and ObserveOn is a very helpful couple when you writing UI applications for at least two reasons:
* we don’t want blocking the UI thread
* but we do need updating UI objects on the UI thread.
Now let’s add QBChatService.getInstance().login(qbUser) to our functional request:
Ok, our code looks nice, but what if we need to get array with objects and do some work with it? For example, get ChatDialogs. Let’s get try:
And the same with lambdas:
It’s hard to argue that this code looks just splendid!
And in conclusion let’s combine all this stuff together: QBUsers.signIn, QBChatService.login, QBRestChatService.getChatDialogs and getting each QBChatDialog. Now we have:
P.S And for a titbit, let’s do parallel requests for signIn and login to chat with operator zip:
And that is not all! There are many other useful operators in the RX like GroupBy, Map, Scan, and many more.
With QuickBlox and RXAndroid you can make indeed effective code, sky is the limit!
For more information, visit QuickBlox blog and documentation.
Happy coding!