所以我试图理解GCD。我有一个长时间运行的操作,在我下载后只是附加数据:
NSFileManager *fileManager = [NSFileManager defaultManager];
__block NSFileHandle *output;
output = [NSFileHandle fileHandleForUpdatingAtPath:tempPath];
__block NSError *error = nil;
BOOL success;
dispatch_queue_t stitchQueue = dispatch_queue_create("com.test", NULL);
for (NSString *packetName in listOfFiles) {
dispatch_async(stitchQueue, {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *packetPath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:packetName];
NSData *packetData = [[NSData alloc] initWithContentsOfFile:packetPath];
[output seekToEndOfFile];
[output writeData:packetData];
[fileManager removeItemAtPath:packetPath error:&error];
NSLog(@"Removed file after appending data success: %i Error: %@", success, [error localizedDescription]);
[self updateManifestColumn:@"IsParsed" withValue:YES forFile:packetName inTable:tableName];
packetData = nil;
[packetData release];
[pool release];
});
}
[output closeFile];
//dispatch_async( // 数据拼接后执行我的下一个长期任务)
如果我删除 dispatch_async 调用,此代码将起作用。难道我做错了什么?当我使用 dispatch_async 运行它时,它成功完成了一次迭代然后崩溃。它在 NSFileHandle 上的错误访问时崩溃。它似乎在 1 次迭代后被释放。我不确定我需要做什么来解决这个问题。谢谢!