我有这个代码:
__block NSMutableArray *subCategoriesBlock = self.subCategories ;
__block UITableView *lstSubCategoriesBlock = self.lstSubCategories;
[[AsyncRequest initRequest: onCompletedBlock:^(NSMutableArray *subcategories) {
[subCategoriesBlock addObjectsFromArray:subcategories];
[lstSubCategoriesBlock reloadData];
}]ExecuteRequest];
AsyncRequest 类将在后台发送 http 请求,然后运行 onCompletedBlock。
我的问题:
有时系统会释放当前 View Controller ,然后调用 onCompleted block ,并在 [lstSubCategoriesBlock reloadData] 上崩溃,因为它已经被释放了。
我应该怎么做,以防止系统在releaasd viewContorller时执行[lstSubCategoriesBlock reloadData] ?
Best Answer-推荐答案 strong>
使用 __weak 以便在对象被释放时指向 nil
__block __weak NSMutableArray *subCategoriesBlock = self.subCategories ;
__block __weak UITableView *lstSubCategoriesBlock = self.lstSubCategories;
[[AsyncRequest initRequest: onCompletedBlock:^(NSMutableArray *subcategories) {
[subCategoriesBlock addObjectsFromArray:subcategories];
[lstSubCategoriesBlock reloadData];
}]ExecuteRequest];
关于ios - 调用 dealloc 后如何停止运行 block ? IOS,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/25524012/
|