所以我想了解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