假设我有一个类(非 ARC 环境):
@interface SomeObject : NSObject {
UILabel *someLabel;
dispatch_queue_t queue;
}
- (void)doAsyncStuff;
- (void)doAnimation;
@end
@implementation SomeObject
- (id)init {
self = [super init];
if (self) {
someLabel = [[UILabel alloc] init];
someLabel.text = @"Just inited";
queue = dispatch_queue_create("com.me.myqueue", DISPATCH_QUEUE_SERIAL);
}
return self;
}
- (void)doAsyncStuff {
dispatch_async(queue, ^{
...
// Do some stuff on the current thread, might take a while
...
dispatch_async(dispatch_get_main_queue(), ^{
someLabel.text = [text stringByAppendingString:@" in block"];
[self doAnimation];
}
}
}
- (void)doAnimation {
...
// Does some animation in the UI
...
}
- (void)dealloc {
if (queue) {
dispatch_release(queue);
}
[someLabel release];
[super dealloc];
}
如果我的块被启动,然后所有其他持有对该对象实例的引用的东西都释放它,我是否保证不会调用 dealloc,因为嵌套块引用了一个实例变量(和 self)——那个 dealloc嵌套块退出后会发生吗?我的理解是我的块对自我有很强的参考,所以这应该是犹太洁食。