【发布时间】:2014-07-17 13:59:22
【问题描述】:
关于所有这些弱者和强者有很多问题,但我希望你们看看我的具体例子:
- (void)getItemsWithCompletionHandler:(void (^)(NSArray*items))completionHandler {
__weak __typeof__(self) weakSelf = self;
[self doWorkWithCompletionHandler:^(Response *response) {
// this completion is not on main thread
dispatch_async(dispatch_get_main_queue(), ^{
...
[weakSelf doAnotherWorkWithCompletionHandler:^(Response *response) {
// this completions is not on main thread either
dispatch_async(dispatch_get_main_queue(), ^{
__typeof__(self) strongSelf = weakSelf;
NSArray *itemsIds = [strongSelf doWorkOnMainThread1];
NSArray *items = [strongSelf doWorkOnMainThread2];
completionHandler(items);
});
}];
});
}];
}
这里的一切都正确吗?也欢迎您提出重构建议
【问题讨论】:
-
这个问题在Codereview上排序可能会更好
-
在外部本地范围内声明
itemsIds和items有什么意义?在异步操作中分配这两个变量时,范围并不存在。 -
是什么让你觉得这里需要弱引用?
-
问题主要是关于块内存管理而不是重构!
标签: ios objective-c memory-management objective-c-blocks