Unzipping file on the device crashes due to timeout
I am trying to unzip a file which is being downloaded through URL. The file downloaded is in zip format. I have used library : iPhone Unzip code which has SSZipArchive Library. It is used in current code to unzip the file once downloaded. But when I run the app as a stand alone application crashes by giving the log errors : example.app failed to launch in time. I am not sure if I can use this library or is there any other library which has been tried, tested and have it running on the device.
- (void)viewDidLoad {
[super viewDidLoad];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *directoryPath = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/ZipFiles.zip", directoryPath];
NSString* updateURL = @"http://www.example.com/test.php?id=11";
NSData* updateData = [NSData dataWithContentsOfURL: [NSURL URLWithString: updateURL] ];
[fileManager createFileAtPath:filePath contents:updateData开发者_开发百科 attributes:nil];
/* SSZipArchive is a free API built on ZipArchive to unzip the files. Header file is passed in this class.*/
[SSZipArchive unzipFileAtPath:filePath toDestination:directoryPath];
NSLog(@"Finished unzipping database");
}
The viewDidLoad method is being called from the main thread of the application and since the download and unzip operations are taking a significant amount of time, it is freezing the main thread. This is why you are seeing that error.
You need to move long running operations to a background thread so that the main thread is free to finish loading the application. There are many ways to do this and I prefer using blocks.
I suggest you read the Concurrency Programming Guide in the docs:
http://developer.apple.com/library/ios/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008091
精彩评论