RxSwift: Is it safe to always use [unowned self] when a class has a disposeBag property?
我最近发现一篇文章说,只要您将订阅添加到 DisposeBag 并且它位于视图控制器内,使用 [unowned self] 总是安全的。
假设我有一个 ViewController,其中 deinit 由于强引用而没有被调用:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
class ViewController: UIViewController {
@IBOutlet weak var searchBar: UISearchBar! private let disposeBag = DisposeBag() override func viewDidLoad() { tableView.rx.itemSelected searchBar.rx.text private func search(query: String) { // Some logic here… |
我应该能够简单地在订阅闭包中声明 [unowned self],而不必担心我的应用会因 self 是 nil 而崩溃。
我感到困惑的是,因为搜索是异步的,这是否意味着如果 ViewController 在查询完成之前已从导航堆栈中弹出,那么 self 可以是 nil 吗?
或者 DisposeBag 会先被释放而闭包不会完成?
任何关于如何知道一个类是否拥有闭包的说明也很好。
- 我想你不能这取决于内部实现和线程竞争。例如,如果 http 客户端捕获了您的闭包并调用了请求,那么即使 disposeBag 取消订阅,仍然可能存在竞争条件,并且 http 客户端可能会在处理 self 后立即调用您的闭包。
根据我的经验,将 unowned 与处置袋一起使用是一种安全的方法,除了一个块 – onDisposed。有一些应用程序因为 unowed 关键字而崩溃的情况 -> weak 在这里很有用。
正如@kzaher 在github上所说的那样
you should never use unowned.
来源:
https://github.com/RxSwiftCommunity/RxDataSources/issues/169
https://github.com/ReactiveX/RxSwift/issues/1593
来源:https://www.codenong.com/46844148/