Anatomy of rx.Observable
Indeed,Observablecan actually produce three types of events:
- Values of type T,as declared by Observable
- Completion event
- Error event
Subscribing to Notifications from Observable
Controlling Listener by Using Subscription and Subscriber
Creating Observables
1、Mastering Observable.create()
Managing Multiple subsscribers
- cache()
2、Infinite Streams
Two techniques for subscriber to clean up resource when unsubscribes
- if(subscriber.isUnsubscribed()){…}
- subscriber.add(Subscriptions.create(…))
You should not use explicit threads inside create()
It is a good practise to wrap entire expressions within create() in a try-catch block.Throwables should be propagated downstream rather than logged or rethrown.
Timing:timer() and interval()
- timer()
- interval()
Hot and Cold Observables
Observable | cold | hot |
---|---|---|
deliver of events | receive a complete and consistent set of events | don`t know whether received all events from the beginning |
time dependency | the exact instant when an item was created is irrelevant | represent events as they come,typically from some external source |
How to multiplex a single upstream source to multiple Observers
1、rx.subjects.Subject
Subjects are useful,but there are many subtleties you must understand.For example,after calling subject.onError(),the Subject silently drops subsequent onError notifications,effectively swallowing them
- PublishSubject
- AsyncSubject
- BehaviorSubject
- ReplaySubject
2、ConnectableObservable
Single Subscription (Observable.share())
No matter how many Subscribes connect to ConnectableObservable,it opens just one subscription to the Observable from which it was created
- Observable.publish()
- ConnectableObservable.refcount();
ConnectableObservable Lifecycle
Force subscription in absence of any Subscriber
- Observable.publish()
- ConnectableObservable.connect();
autoConnect() (since 1.2)
- Observable.publish()
- ConnectableObservable.autoConnect();
最后,安利一款自己写的基于MVP的Android开发框架
https://github.com/sxenon/Pure
欢迎大家拍砖,如果觉得好,麻烦多多star