Observer
And Subscriber
it is exactly the same. They differ mainly two things for the user:
onStart()
: This is theSubscriber
added methods. It just started subscribe, to be called before the event has not been sent, can be used to do some preparatory work, such as clear or reset data. This is an optional method, by default, its implementation is empty. Note that, if required (for example, pops up a dialog box displays progress, which must be executed in the main thread) on the thread is ready to work,onStart()
it does not apply, because it is always called in the thread subscribe occurred, and You can not specify thread. To do the preparatory work in the specified thread, you can usedoOnSubscribe()
the method, specific can be seen in the back of the text.unsubscribe()
: This isSubscriber
another interface implementedSubscription
method for unsubscribe. After this method is called,Subscriber
it will no longer receive event. General before this method is called, can be usedisUnsubscribed()
to determine what status.unsubscribe()
This method is very important, becausesubscribe()
after,Observable
will holdSubscriber
a reference to this quote if you can not be released, there is a risk of memory leaks. So it is best to keep one principle: to be in the right place (for example, as soon as possible when no longer in useonPause()
onStop()
and other methods) to callunsubscribe()
to dereference relations, in order to avoid memory leaks.
Author: Scus
link: https: //juejin.im/post/5a224cc76fb9a04527256683
Source: Nuggets
copyright reserved by the authors. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.