简体   繁体   中英

ZipArchive blocking main thread

when using ZipArchive to unzip a package its seems to be blocking the main thread. theres about 283 files in the zip file. I'm throwing it on the background thread but it doesn't seem to be helping.

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;
}

this code above freezes the screen for about 5 seconds when unzipping. i was assuming that throwing it on the background thread would help but it didn't. any help would be awesome!

 @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);
      }   
  }

If "self" in this context is your View/ViewController, you should consider using another variable in the synchronized block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM