This article is My RxSwift Summary ①, My RxSwift Summary ②, My RxSwift Summary ③ (What is Observable?) is a continuation.
subscribe
/ How to end is dispose bag
ʻObservable has a feature that "it has 3 events (
next / error / completed), and by subscribing, it becomes possible to handle those elements ". There is a
subscribe` to start, but how should I end it? There are three possible ways to start it.
--ʻErrorEvent is flowing --The
completed event is flowing --Add ʻobservarble
to dispose bag
This time, I will describe the third dispose bag
in detail.
dispose bag
?Garbage bag for discarding ʻobservable`s at once
Some ʻobservables do not want to play
completed or ʻerror
(ex: 〇〇). In that case, it is necessary to explicitly complete (≒ discard) ʻobservable by using the
dispose method provided in ʻobservable
. (If you don't complete it, you'll be in a subscribe
state forever, causing a memory leak.)
At that time, instead of using the dispose
method individually for the ʻobservable, have a
disposebag as a property of the instance that puts the ʻobservable
together in order to reduce the management cost, and all at the timing of instance destruction. ʻObservable can be
dispose(I often see patterns that have it as a property of
ViewController`)
Recommended Posts