I have an app where users can receive notifications based on topics they subscribe to. The workflow is the following:
- User logs in
- The app registers to the notification server
- The users chooses to subscribe/unsubscribe from various topics
I want all the network requests to be serialized. If I knew at initialization what exact topics are going to be subscribed/unsubscribed, I could write a stream like below:
loginObservable.subscribeOn(Schedulers.io())
.flatMap(user -> registerApp(appId))
.flatMap(o -> subscribeToTopic("topic1"))
.flatMap(o -> unsubscribeFromTopic("topic2"))
.observeOn(AndroidSchedulers.mainThread())
.subscribe()
The thing is that users can subscribe/unsubscribe at any point in the app lifecycle, possible even before registration is successful. I could maintain a list of observable and serialize all the requests by hand in onComplete() but that doesn't sound very Rx-ish. Any chance I could do it in a more concise way ? Something like:
observable = loginObservable.subscribeOn(Schedulers.io())
.flatMap(user -> registerApp(appId))
.observeOn(AndroidSchedulers.mainThread())
.subscribe()
//later
observable.flatMap(o -> subscribeToTopic("topic1"))
.subscribe()
//even later
observable.flatMap(o -> unsubscribeFromTopic("topic2"))
.subscribe()