简体   繁体   English

RxSwift DisposeBag在ViewController中的用法

[英]RxSwift DisposeBag usage in ViewController

I am new to the RxSwift framework. 我是RxSwift框架的RxSwift I am using disposables in my ViewController and I am adding disposables in a DisposeBag . 我在ViewController使用一次性用品,并在DisposeBag添加一次性用品。

Where should I deallocate the DisposeBag in order to dispose of all disposables? 我应该在哪里分配DisposeBag以便处置所有一次性物品? In the controller's viewDidAppear or deinit ? 在控制器的viewDidAppeardeinit Which method is safer? 哪种方法更安全?

If you declare your dispose bag as an instance variable of your view controller subclass, it will be deallocated automatically as soon as your view controller gets deallocated. 如果您将处理包声明为视图控制器子类的实例变量,则将在视图控制器被释放后立即自动将其释放。 That is, if it is not retained by something else as well. 也就是说,如果它也没有被其他东西保留。

What is the purpose of a Disposable 一次性用品的目的是什么

Disposables are here to represent a handle into the observable's subscription. 此处的可售商品代表可观察对象的订购的句柄。 When disposed, it cancels the observable's operation. 处置后,它将取消可观察对象的操作。 The most straight forward example is a network request. 最直接的例子是网络请求。 When the disposable related to this request is disposed of, if the request did not complete, it is cancelled. 当处理与该请求有关的一次性用品时,如果请求未完成,则将其取消。

DisposeBag DisposeBag

Dispose bag gathers multiple diposables whose lifecycles should be related. 处置袋收集多个生命周期应相关的一次性物品。 When the bag gets disposed of, all the diposables within it are also disposed of. 当袋子被处置时,其内的所有可降解物也被处置。

Where does it make sense to dispose of a bag within a View Controller 在View Controller中将袋子放置在哪里有意义

Now that we know what the disposables actually do, the question we need to answer is no longer "where should I dispose of my bag", but "when does it make sense to cancel my subscriptions"? 既然我们知道了一次性用品的实际用途,我们需要回答的问题不再是“我应该在哪里处置行李”,而是“何时取消我的订阅”?

And here the answer is really related to the use case : sometimes you'll want to stop any work if the view controller is not on screen anymore. 在这里,答案实际上与用例有关:有时,如果视图控制器不再在屏幕上,您可能希望停止任何工作。 In that instance, releasing the dispose bag within viewDidDisappear: is a good option. 在这种情况下,释放viewDidDisappear:的处理包是一个不错的选择。 Other times, it is probably better to let the dispose bag get released in deinit (classes lifecycle will take care of this without you needing to override deinit though), in the instances where you would rather have the observable continue do its work even if the view controller is absent from the screen. 在其他情况下,最好在deinit释放处理包(类生命周期将解决此问题,尽管您无需覆盖deinit),在这种情况下,您宁愿观察到的继续执行其工作,即使屏幕上没有视图控制器。

In conclusion, no method is safer than another, it only depends on your use case. 总之,没有一种方法比其他方法更安全,它仅取决于您的用例。

If you subscribe to your observables in the viewWillAppear function, then you should deinit your disposeBag in the viewDidDisappear function. 如果您在viewWillAppear函数中订阅了Observable,则应在viewDidDisappear函数中取消初始化disposeBag。 If you subscribe to your observables in the viewDidLoad , then don't worry about it, the dispose bag will automatically dispose. 如果您在viewDidLoad订阅了您的可观察对象,那么不用担心,dispose包将自动进行处置。 This latter way is standard. 后一种方法是标准的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM