#pragma mark - NSURLSessionDownloadDelegate
- (void)URLSession:(NSURLSession *)session
downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location
{
...
[[NSFileManager defaultManager] moveItemAtURL:location toURL:fileURL error:&error];
...
}
当子线程在使用NSFileManager移动文件,而主线程也在使用它移动文件时,发生EXC_BAD_EXEC
类型Crash。
因为我们常使用的NSFileManager都是单例[NSFileManager defaultManager]
所以使用同一个单例在两个线程中同时移动文件造成了崩溃。
应该在子线程中使用[[NSFileManager alloc] init]
,这个方法创建的实例是线程安全的。
In iOS and Mac OS X v 10.5 and later you should consider using [[NSFileManager alloc] init] rather than the singleton method defaultManager. Instances of NSFileManager are considered thread-safe when created using [[NSFileManager alloc] init].