首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ZipArchive阻塞主线程

ZipArchive阻塞主线程
EN

Stack Overflow用户
提问于 2014-05-22 22:52:28
回答 1查看 175关注 0票数 1

当使用ZipArchive解压一个包时,它似乎阻塞了主线程。压缩文件中大约有283个文件。我把它放在后台线程上,但它似乎没有帮助。

代码语言:javascript
复制
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秒。我以为把它放在后台线程上会有帮助,但它没有。任何帮助都会很棒!

EN

回答 1

Stack Overflow用户

发布于 2014-05-22 23:16:10

代码语言:javascript
复制
 @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块中使用另一个变量。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23810270

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档