私のアプリが初めて起動するとき、ドキュメントフォルダーにいくつか(約300〜400枚の画像)をコピーする必要があります。私が見ているのは、ツールに時間がかかるということです(今は30〜40枚の画像だけでテストしていると思っていましたが)。最初に、実行に時間がかかりすぎたため、(シミュレーターではなく) 電話で実行するとアプリがクラッシュしました。現在、スレッド上のすべてのファイルをコピーするメソッドを実行しています。アプリは起動したままですが、数秒後にiosがそのスレッドを強制終了すると思います。各画像のコピーを新しいスレッドに置く必要がありますか???
私のコードは次のようなものです:(これはスレッドで実行される部分です)
-(void) moveInitialImagesFromBundleToDocuments {
//move all images.
NSMutableArray *images = [MyParser getAllImagesList];
for (int i = 0 ; i< [images count] ; i++) {
[self copyFileFromBundleToDocuments:[images objectAtIndex:i]];
}
}
- (void) copyFileFromBundleToDocuments: (NSString *) fileName {
NSString *documentsDirectory = [applicationContext getDocumentsDirectory];
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSLog(@"Source Path: %@\n Documents Path: %@ \n Destination Path: %@", sourcePath, documentsDirectory, destinationPath);
NSError *error = nil;
[self removeFileFromPath:destinationPath];
[[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:destinationPath error:&error];
NSLog(@"File %@ copied", fileName);
NSLog(@"Error description-%@ \n", [error localizedDescription]);
NSLog(@"Error reason-%@", [error localizedFailureReason]);
}
助言がありますか?まず、コピーを高速化するために、次に、コピーするファイルごとに新しいスレッドを作成する必要がありますか?
コメント 1:
これは良さそうです。しかし、すべての画像が読み込まれるまでユーザーがアプリケーションを使用できないように、アプリケーションを暗くしたいと思います。
このメソッドを の下で実行します。ユーザーがアプリケーションを実行できなくなりますが、試してみると読み込みに時間がかかりすぎたため、電話ではスレッドが殺されていると思います。実際に停止したことはありません。
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = @"preparing...";
hud.dimBackground = YES;
hud.square = YES;
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
// Do something...
FileUtil *fileut = [[FileUtil alloc] init];
[fileut moveInitialImagesFromBundleToDocuments];
//Done something
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
});