简体   繁体   中英

RxSwift Driver weakly subscription?

I was looking for an easier way to pass function as parameter into Observable's subscribe method without causing retain cycle; I don't want to always have to use [weak self] every single time. I've just come across this answer , and it's exactly what I want. Even better, it's already being merged into the framework itself. However, I don't seem to find a similar method for Driver .

Has it already been implemented, and I'm just looking at the wrong place? If not, can you guide me how to add it? ... As a side note, how do you normally deal with all the [weak self] everywhere?

As a side note, how do you normally deal with all the [weak self] everywhere?

I rarely use subscribe , so I don't need [weak self] very often. Use bind(to:) as much as possible and you don't have to worry about self. Another option is to make a local reference and use it. For example:

let foo = self.foo
myObservable.subscribe(onNext: {
    // Use of 'foo' here will refer to the local foo, not self.foo.
    // Of course if foo is a class type, they will both refer to the 
    //   same object so that's fine.
    // No weak self necessary because self isn't part of the chain.
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM