Search code examples
iosmemory-managementrx-swiftswift4.2retain-cycle

RxSwfit bind operation retain cycle


I am new at rxswift framework.I have written a code below and I am not sure.Is there any retain cycle? Must I use weak reference to self?

    loginButton.rx.tap.bind {
        print(self.nameText.value ??  "")
       self.nameText.accept("ahmet vefa saruhan")
    }.disposed(by: disposebag)

second case is :

func myTestFunction(handler : () -> Void) {
    handler()
}

myTestFunction {
        self.isVisible = false
    }

is here any retain cycle problem?


Solution

  • Yes, weak should be used when you using self inside block. And use subscribe instead of bind.

    basicLoginButton.rx.tap.subscribe(onNext: { [weak self] () in
        print(self?.nameText.value ?? "")
        self?.nameText.accept("ahmet vefa saruhan")
    }).disposed(by: disposeBag)
    

    I think in the second case, everything is correct, there is not needed 'weak'.