2011-07-11 37 views
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"); 
} 

回答

0

viewDidLoad方法正在從應用程序的主線程,並且由於下載打來電話,解壓縮操作正在服用的時間顯著量,它被凍結的主線程。這就是你看到這個錯誤的原因。

您需要將長時間運行的操作移至後臺線程,以便主線程可以自由完成加載應用程序。有很多方法可以做到這一點,我更喜歡使用塊。

我建議你在閱讀文檔併發編程指南:

http://developer.apple.com/library/ios/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008091

相關問題