【发布时间】:2013-12-30 22:08:33
【问题描述】:
下面的设计模式在我的应用中出现了几次。我正在转换为 ARC。有人可以证实__unsafe_retained __block 的用法是否正确吗?
__unsafe_unretained __block id observer = [[NSNotificationCenter defaultCenter] addObserverForName:MyNotification
object:nil
queue:nil
usingBlock:^(NSNotification *note) {
[[NSNotificationCenter defaultCenter] removeObserver:observer];
// ...
}];
一些注意事项:
-
使用
__unsafe_unretained,因为部署目标是 10.6 -
使用
__block是因为否则observer会被块过早捕获 - Xcode 的 ARC 迁移工具为我添加了
__unsafe_unretained。有必要吗?我意识到没有它,该块将保留observer,但这有那么糟糕吗?会导致保留周期吗?
【问题讨论】:
标签: objective-c automatic-ref-counting objective-c-blocks nsnotificationcenter weak-references