在几乎每个 UIViewController
中,我都有一堆 AFHTTPRequestOperation
,我必须正确处理任何类型的取消(按下取消按钮,返回 UINavigationController
的堆栈等)。我想知道是否为每个 UIViewController
创建一个 NSOperationQueue
并将 Controller 内调用的所有操作添加到其中是否是一种正确的方法?我的目标是取消所有操作 [[NSOperationQueue mainQueue] cancelAllOperations]
但这会终止所有已经开始的操作,尤其是那些从之前的 UIViewController
调用的操作。或者我应该为每个操作创建属性,在 viewWillDissappear:(BOOL)animated
中调用它并在成功 block 中设置取消状态的 if 语句?
最佳答案
AFHTTPRequestOperationManager
实例的创建成本很低,每个实例都有自己的操作队列,因此很容易取消给定 UIViewController
的所有操作:
- (void)dealloc {
[self.requestOperationManager.operationQueue cancelAllOperations];
}
这将取消通过 self.requestOperationManager
创建的任何请求。您可以在 UIViewController
的 init 方法中创建 AFHTTPRequestOperationManager
。
我建议取消 View Controller 的 dealloc 方法中的操作,因为您知道它不再需要了。
关于ios - 我应该为每个 UIViewController 创建一个 NSOperationQueue 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20286905/