Using 'self' on RxSwift closures… What about instance methods as param?

白昼怎懂夜的黑 提交于 2019-12-20 03:24:15

问题


In other stack overflow questions, it was emphasized that the capture [weak self] should be used for closures that aren't owned by the class because self could be nil before the closure completes. An alternative when the closure is owned by the class itself is [unowned self].

My question is do I need to use [unowned self] when the function I pass as a parameter is an instance method of the current class?

Example

import RxSwift

class Person {
    var name = "Default name"

    class func getPersons() -> Observable<Person> {
        // ...
    }


}

class MyController: UIViewController {
    let disposeBag = DisposeBag()

    // I know this right
    func unownedDisplayPeople() {

        Person.getPersons()
            .subscribeNext { [unowned self ] person in
                self.displayName(person)
            }
            .addDisposableToBag(disposeBag)
    }

    // But what about this?
    func whatAboutThisDisplayPeople() {

        Person.getPersons()
            .subscribeNext(displayName)
            .addDisposableToBag(disposeBag)
    }

    // Or this?
    func orThisDisplayPeople() {

        Person.getPersons()
            .subscribeNext(self.displayName)
            .addDisposableToBag(disposeBag)
    }

    func displayName(person: Person) {
        print("Person name is \(person.name)")
    }
}

If I still need to think about the reference counting when I just pass an instance method, how do I do it? Where do i put the [unowned self]? Or is it considered [unowned self] already when I just pass the instance method?


回答1:


Unfortunately, passing an instance method to subscribeNext will retain self. To be more generic, storing a reference to an instance method will increase the retain count of the instance.

let instance = ReferenceType()
print(CFGetRetainCount(instance)) // 1

let methodReference = instance.method
print(CFGetRetainCount(instance)) // 2

The only solution here is do what you have done in unownedDisplayPeople.

let instance = ReferenceType()
print(CFGetRetainCount(instance)) // 1

let methodReference = { [unowned instance] in instance.method() }
print(CFGetRetainCount(instance)) // 1


来源:https://stackoverflow.com/questions/40583685/using-self-on-rxswift-closures-what-about-instance-methods-as-param

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!