所以我想了解GCD。我只是将数据追加一个长期运行的操作我下载这样的后:for dispatch_async/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次后解除分配。我不知道我需要做什么来解决这个问题。谢谢!
您是否尝试过用'dispatch_async'封装整个'for(..)'? – Johnnywho 2012-08-10 08:13:08
@Johnnywho是的,这也不起作用。 – Crystal 2012-08-10 18:20:48