gpt4 book ai didi

ios - ZipArchive 阻塞主线程

转载 作者:行者123 更新时间:2023-11-29 12:45:19 30 4
gpt4 key购买 nike

当使用 ZipArchive 解压缩包时,它似乎阻塞了主线程。 zip 文件中大约有 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 秒钟。我假设将它放在后台线程上会有所帮助,但事实并非如此。任何帮助都是极好的!

最佳答案

 @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 block)中使用另一个变量。

关于ios - ZipArchive 阻塞主线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23810270/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com