@interface ClassA : NSObject
@property (strong, nonatomic) dispatch_queue_t dispatchQ;
@property (strong, nonatomic) NSString *string;
@end
@implementation ClassA
- (id)init
{
self = [super init];
if (self) {
_dispatchQ = dispatch_queue_create("com.classa.q", NULL);
}
return self;
}
- (void)longRunningTaskWithCompletion:(void(^)(void))completion
{
dispatch_async(self.dispatchQ, ^{
for (int i = 0; i < 10000; i++) {
NSLog(@"%i", i);
}
dispatch_sync(dispatch_get_main_queue(), ^{
self.string = @"Class A Rocks!";
if(completion) {
completion();
}
});
});
}
@end
我认为这段代码创建了一个保留周期,因为块中的块在块中-longRunningTaskWithCompletion:
捕获自我(设置字符串属性)并将块添加到调度队列属性中。