当使用ZipArchive解压一个包时,它似乎阻塞了主线程。压缩文件中大约有283个文件。我把它放在后台线程上,但它似乎没有帮助。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, (unsigned long) NULL), ^{
[self tryUnzipFile:fileName inContentPackage:contentPackage];
});
- (void)tryUnzipFile:(NSString*)fileName inContentPackage:(MKContentPackage*)contentPackage {
@synchronized (self) {
NSString *filePath = [contentPackage zipFilePathForFile:fileName];
BOOL unzipSucceeded = [self unzipFile:filePath toFolder:contentPackage.unzipFolder];
if (unzipSucceeded) {
[self excludeFromBackup:contentPackage.downloadFolder];
NSLog(@"Content: Unzipping Content Package: %@ FileName: %@", contentPackage.identifier, fileName);
}
}
}
- (BOOL)unzipFile:(NSString*)zipFilePath toFolder:(NSString*)zipFolder {
ZipArchive *zipArchive = [[ZipArchive alloc] init];
NSString* unzipPath = [NSObject documentFolderFrom:zipFolder fileName:@""];
// Do the unzipping
[zipArchive UnzipOpenFile:zipFilePath];
BOOL unzipped = [zipArchive UnzipFileTo:unzipPath overWrite:YES];
[zipArchive UnzipCloseFile];
if (unzipped) {
[self removeZipPackage:zipFilePath];
}
return unzipped;
}上面的代码在解压时冻结屏幕大约5秒。我以为把它放在后台线程上会有帮助,但它没有。任何帮助都会很棒!
发布于 2014-05-22 23:16:10
@synchronized (self) {
NSString *filePath = [contentPackage zipFilePathForFile:fileName];
BOOL unzipSucceeded = [self unzipFile:filePath toFolder:contentPackage.unzipFolder];
if (unzipSucceeded) {
[self excludeFromBackup:contentPackage.downloadFolder];
NSLog(@"Content: Unzipping Content Package: %@ FileName: %@", contentPackage.identifier, fileName);
}
}如果这个上下文中的"self“是您的View/ViewController,那么您应该考虑在synchronized块中使用另一个变量。
https://stackoverflow.com/questions/23810270
复制相似问题