在设备上解压文件由于超时而崩溃
我正在尝试解压缩通过 URL 下载的文件。下载的文件为 zip 格式。我使用过库: iPhone Unzip code ,它有 SSZipArchive 库。当前代码中使用它来解压缩下载后的文件。但是,当我将应用程序作为独立应用程序运行时,会因给出日志错误而崩溃: example.app 未能及时启动。我不确定我是否可以使用这个库,或者是否有任何其他库已经过尝试、测试并在设备上运行。
- (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");
}
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");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
viewDidLoad 方法是从应用程序的主线程调用的,由于下载和解压缩操作花费了大量时间,因此它冻结了主线程。这就是您看到该错误的原因。
您需要将长时间运行的操作移至后台线程,以便主线程可以自由地完成应用程序的加载。有很多方法可以做到这一点,我更喜欢使用块。
我建议您阅读文档中的并发编程指南:
http://developer.apple.com/library/ios/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008091
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