Q:

Iphone - > Copying images from bundle to documents folder takes TOO long

whem my app starts for the first time I need to copy some ( arround 300-400 images ) to the documents folder. What I see is that it takes tool long (even thought now Im testing only with just 30-40 images). At the begining my app crashed when running on the phone (not on simulator) because it took too long to run. Now I'm running the method that copies all files on a thread. The app stays up, but I think that ios kill that thread after a few seconds. Should I have each image copy on a new thread???
My code is like this: (this is the part that runs in the thread)
-(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]);
}

Any suggestions? First to make copying faster and second, should I make a new thread for each file I copy?

Comment 1:
This looks nice. But I would like to dim the application so the user will not be able to use the appication untill all images are loaded.
I run this methods under . It does stop the user from being able to run the application, but I think that on the phone , the thread is beeing killed because when I tried it , it took too long loading. Never stopped actualluy.
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];
    });
});

A:

When calling the method, I suggest you to use GCD to move this to a background thread, so you could call the whole for cycle like this. (I also changed a little bit your for cycle to make it simple.
-(void) moveInitialImagesFromBundleToDocuments
{
//move all images and use GCD to do it

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
    NSMutableArray *images = [MyParser getAllImagesList];
        for (id image in images) {
            [self copyFileFromBundleToDocuments:image];
        }
    });
}

Regarding to make the copying faster, I don't know of any solution.

