假设我有一个类(非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会在嵌套块退出后发生?我的理解是,我的街区对自我很有借鉴意义,所以这应该是犹太教。
这就是为什么我更喜欢'self-> ivar.property',因为我可以看到它保留了'self'。不仅仅是'ivar.property',它实际上通过'self->来扩展。 – Tricertops
如果自己变成零,那么使用self> ivar是不好的,它会导致崩溃。在块中使用它时最好将其定义为属性,并使用__weak self来防止保留周期。 –