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

前端 未结 1 1835
甜味超标
甜味超标 2021-01-22 21:59

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

相关标签:
1条回答
  • 2021-01-22 22:38

    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
    
    0 讨论(0)
提交回复
热议问题