Take the 2-minute tour ×

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);

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];
share|improve this question
Just why do you need to copy the images? You can access them just fine where they are. If you have a mix of images in /Documents you can always defined some way to deal with "canned" images and other modified ones. You are wasting 200M of space on users devices by duplicating the files. Another idea is to link or "symbolically link" from the documents folder to the bundle - a bit more complicated (have to use the C api I believe) but would be MUCH faster (if it works) as you would be created a file system pointer instead of copying files. –  David H Aug 12 '12 at 19:51
I agree with David H. What's the point of the copy? Pulling images from the bundle is a perfectly legitimate practice as far as I know. –  mark Aug 12 '12 at 20:11
The problem is that user will have to ability to take updates of the data from the internet. So I cannot ovewrite the files that are in the bundle. So I need to have them in the doc folder. –  Panos Aug 12 '12 at 22:58
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
please check comment 1 on my question. –  Panos Aug 12 '12 at 19:07
add comment

Your Answer


