简体   繁体   中英

Understanding retain cycles in RxSwift

If I have the following code

func handle(showEmptyView: Driver<Bool>) {
    showEmptyView
        .drive(onNext: setEmptyViewShown)
        .disposed(by: disposeBag)
}

func setEmptyViewShown(_ show: Bool) {
    tableView.isHidden = !show
    emptyView.isHidden = show
}

Is this a retain cycle when I call setEmptyViewShown because I don't use weak or unowned self?

Yes there is a retain cycle because setEmptyViewShown(_:) is a method which takes self as an implicit first argument.

Better would be something like:

disposeBag.insert(
    showEmptyView.bind(to: emptyView.rx.isHidden),
    showEmptyView.map { !$0 }.bind(to: tableView.rx.isHidden)
)

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