在设备上解压文件由于超时而崩溃

发布于 2024-11-19 18:59:17 字数 1134 浏览 4 评论 0原文

我正在尝试解压缩通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只怪假的太真实 2024-11-26 18:59:17

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文