I have previously used MediatorLiveData to combine results from FCM, Room and Networking in my Repository. It worked well. However, now I want more complexity and some additional bonuses from RxJava. This is why I have decided to use RxJava this time. I can combine Room and Networking with Observable.concatArrayEager(my _observables). However, I don't now how to do that after FCM pushes value, and how should I notify my main observable after new changes occur? No examples on this issue whatsoever. It is crucial part. I receive FCM in my BroadCastReceiver and then notified my repository's livedata, which notified my MediatorLiveData... How to do that with RxJava? Would really welcome any advice, because it is really important issue.
2 Answers
IMHO, you don't need to combine data from different sources.
Using the database as your single source of truth is definitely easier.
Room supports Flowable
like:
@Query(“SELECT * FROM Users WHERE id = :userId”)
Flowable<User> getUserById(String userId);
Each time there is any updates to the table Users
, Room
will emit the updated user automatically once you subscribed.
The following approach may help you:
- In you
Activity / Fragment
, subscribe to theFlowable
. - Once receiving FCM notification, call network api and update the db.
- Do with the updated data.
Anyway, if you need to combine sets of items from different Observables, you can use zip
.

- 1,880
- 1
- 16
- 21
-
Yes, after a lot search I came to conclusion that it is better to use local database as single source of truth... However, I still interested how one can combine different obserables into one observable. I found that only merge works in my case, but it still does not provide latest value, only an old one. – Viktor Vostrikov Aug 25 '18 at 16:10
-
So you need both result of them from two observables? If yes, then you can use `zip` which combine them into one. – jaychang0917 Aug 26 '18 at 01:45
-
No, I need only the newest one. Each of the three observables(networking, room and FCM)holds particular data, the one, which gets newest data should be the right observable if user if online, if he is offline than I will display room observable. – Viktor Vostrikov Aug 26 '18 at 06:50
-
Seems you are selecting observable but combining them. `val selectedObservable = if (...) observableA() else observableB()` and then `val mainObservable = Observable.just(selectedObservable)` – jaychang0917 Aug 26 '18 at 09:46
-
Maybe I was not fluent, but I need to override my MainObservable as soon as another observable changes. Also your approach about saving data to room does not fully work... Room provides only Flowable, while I need to get observable... – Viktor Vostrikov Aug 26 '18 at 10:08
-
I think RxJava may not have such an implementation out of box. Let me know if it exists:) – jaychang0917 Aug 26 '18 at 10:24
-
Ok, I will let you know :) Send request via LinkedIn. By the way, should I save object from fcm push notifications in broadcastreceiver or in FirebaseMessagingService? I think BroadCastReceiver would require more code and as far as I know you should not place expensive tasks there.. – Viktor Vostrikov Aug 26 '18 at 11:07
The concat
operator emit the observables in the order specified, that means the second observable won't begin emitting till the first observable finish emitting objects.
But with merge operator you can combine as many observable you want into a single observable. for example:
Observable.merge(dbObservable, webObservable, fcmObservable)
.subscribe(item -> System.out.println(item));
P.S: In case you want to fetch data from a data source and keep it up-to-date using fcm
you can do this:
fcmObservable
.startWith(dbObservable)
.subscribe(item -> System.out.println(item));
These are just few pattern available, as always there is nothing impossible in RxJava
, based on what you want to do there is always a better RxChain
for the job!

- 3,376
- 1
- 22
- 36