我想返回一个数组,其内容是在 dispatch_sync 块期间设置的。
我通常看到的代码是这样的:
-(NSArray *)getSomeLockedList {
__block NSArray *resultList;
dispatch_sync(myQueue, ^{
// copy contents of my ivar NSMutableArray into return variable
resultList = [ivarContentList copy];
});
// add auto-release since a 'copy' was done within block
return [resultList autorelease];
}
如果我不是在复制一个完整的数组,而是想一个一个地添加,我可以跳过返回值上的“自动释放”吗?
-(NSArray *)getSomeLockedList {
__block NSArray *someResultKeys; // is it ever safe to do the alloc here?
dispatch_sync(myQueue, ^{
someResultKeys = [NSMutableArray array];
for (id entry in ivarContentList) {
// do some work on entry instance
[someResultKeys addObject:entry];
}
});
return someResultKeys; // autorelease not necessary?
}
dispatch_sync 块中的 [NSMutableArray 数组] 分配是否安全,以便在此方法完成堆栈后继续使用结果?