【发布时间】:2014-02-21 23:49:08
【问题描述】:
我正在我的应用程序中处理网络请求,并在 NSOperationQueue 中使用 NSBlockOperations 来异步执行此操作。但是,如果调用它们的视图控制器被释放(已从导航堆栈中弹出),我希望能够取消这些操作。
这是我所拥有的简化版本:
NSArray *posts;
__weak DataController *weakSelf = self;
NSBlockOperation *fetchPostsOperation = [NSBlockOperation blockOperationWithBlock:^{
DataController *strongSelf = weakSelf;
NSDictionary *response = [weakSelf refreshPostsInPart:PartFirst];
posts = [response objectForKey:@"posts"];
}];
[self.queue addOperation:fetchPostsOperation];
在 DataController 的 refreshPostsInPart: 方法中,我使用 while 循环重复网络请求来自 App.net 的分页数据。在循环的每次迭代中,我都会检查 DataController self.isCancelled(类型为 BOOL)的属性,如果是 NO,我会继续发出请求。
在我的 DataController 的 dealloc 方法中,我将此属性设置为 YES,以便在 while 循环的下一次迭代中我将停止发出请求。本质上,我在使用 NSBlockOperation 时实现了一个可怜的人 cancelAllOperations。
问题:在我的 dealloc 方法中将self.isCancelled 设置为NO 时,我是否还为块中使用的strongSelf 引用设置了self.isCancelled?
【问题讨论】:
标签: ios asynchronous objective-c-blocks nsblockoperation strong-references