swift - How do I unsubscribe from an observable? -


if have looks this:

  func foo() -> observable<foo> {     return observable.create { observer in       // ...     }   }    func bar() {     foo().observeon(mainscheduler.instance)       .subscribenext {         // ...       }       .adddisposableto(disposebag)   } 

if want unsubscribe observable later on in bar, how that?

update

i'm aware can call dispose, according rxswift docs:

note not want manually call dispose; educational example. calling dispose manually bad code smell.

so unsubscribe not implemented? i've gone spelunking through rxswift code, , extent can understand what's going on, doesn't disposable returned subscribe methods ever useful functionality (other disposing).

you add observable returned foo disposebag. disposes subscription when it's deallocated. can "manually" release disposebag calling

disposebag = nil

somewhere in class.

after question edit: want selectively unsubscribe observables, when conditions met. can use observable represents these conditions , use takeuntil operator cancel subscription needed.

//given cancellingobservable sends `next` value when subscription `foo` no longer needed  foo().observeon(mainscheduler.instance)   .takeuntil(cancellingobservable)   .subscribenext {     // ...   }   .adddisposableto(disposebag) 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -