Search code examples
iosobjective-cios7ios8

How to unzip asynchronous way zip file in iOS?


How do I unzip a file asynchronously? At the moment I am unzipping like this:

[SSZipArchive unzipFileAtPath:strZipFile toDestination:strDestPath];

But that blocks my main thread and the UI is unresponsive for longer than 10 seconds. The zip file is over 50MB, and I start the download in delegate on didFinishLaunching (to avoid long splash screen because it is not always necessary to have that file at the beginning.)


Solution

  • Run it in a background thread using GCD:

     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [SSZipArchive unzipFileAtPath:strZipFile toDestination:strDestPath];
     });
    

    You might want to tell your app you've finished unzipping by calling a method somewhere in the main thread:

     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [SSZipArchive unzipFileAtPath:strZipFile toDestination:strDestPath];
        dispatch_async(dispatch_get_main_queue(), ^{
            [someClass finishedUnzippingFile:strDestPath];
        });
     });