Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it safe to use [unowned self] in RxSwift Drivers?

Tags:

swift

rx-swift

example:

tapGestureRecognizer.rx.event.asDriver()
    .drive(onNext: { [unowned self] _ in
        self.view.endEditing(true)
    })
    .disposed(by: disposeBag)

since the disposeBag is controlled by self, I would assume yes?

like image 987
amleszk Avatar asked Apr 23 '16 01:04

amleszk


People also ask

Do we need to use weak self or unowned self in this closure?

For many of us, it's best practice to always use weak combined with self inside closures to avoid retain cycles. However, this is only needed if self also retains the closure. By adding weak by default you probably end up working with optionals in a lot of cases while it's actually not needed.

Why do we need unowned Swift?

Unowned references, like weak references, do not increase the retain count of the object being referred. However, in Swift, an unowned reference has the added benefit of not being an Optional. This makes them easier to manage rather than resorting to using optional binding.


1 Answers

Yes, if disposeBag is an member variable of self, that is safe.

like image 65
louoso Avatar answered Sep 29 '22 14:09

louoso