0

URLからダウンロードしているファイルを解凍しようとしています。ダウンロードされたファイルはzip形式です。私はライブラリを使用しました:SSZipArchiveライブラリを持つiPhone解凍コード。現在のコードでは、ダウンロード後にファイルを解凍するために使用されます。しかし、スタンドアロンとしてアプリを実行すると、ログエラーが発生してアプリケーションがクラッシュします。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");
 }
4

1 に答える 1

0

アプリケーションのメイン スレッドから viewDidLoad メソッドが呼び出されており、ダウンロードおよび解凍操作にかなりの時間がかかっているため、メイン スレッドがフリーズしています。これが、そのエラーが表示される理由です。

メイン スレッドが自由にアプリケーションのロードを完了できるように、長時間実行される操作をバックグラウンド スレッドに移動する必要があります。これを行うには多くの方法がありますが、私はブロックを使用することを好みます。

ドキュメントの Concurrency Programming Guide を読むことをお勧めします。

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

于 2011-07-11T22:09:48.273 に答える