1

we are currently developing an enterprise app for our company.The app is supposed to work offline, This means we need to download more than 10K product images to the iPad.Each image can be from several KB to 4M.For doing so at first we used the following code to download images:

-(BOOL)DownloadFileInBatch :(NSArray*) fileArr
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentDirectory = [paths objectAtIndex:0];

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    queue.maxConcurrentOperationCount = 4;

    NSBlockOperation *completionOperation = [NSBlockOperation blockOperationWithBlock:^{
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [[NSNotificationCenter defaultCenter] postNotificationName:@"taskDone" object:self];
        }];
    }];

    @autoreleasepool
    {
        NSBlockOperation *operation;

        for(int i = 0; i < fileArr.count; i++)
        {
             operation = [NSBlockOperation blockOperationWithBlock:^{

                NSURL *url = [[NSURL alloc] initWithString:[[fileArr objectAtIndex:i] objectForKey:@"image_url"]];

                NSData *data = [NSData dataWithContentsOfURL:url];

                NSString *filename = [documentDirectory stringByAppendingString:[NSString stringWithFormat:@"/%@", [url lastPathComponent]]];

                NSLog(@"downloading file : %@ : no : %d", [documentDirectory stringByAppendingString:[NSString stringWithFormat:@"/%@", [url lastPathComponent]]], i);

                BOOL success = [data writeToFile:filename atomically:YES];

                if(!success)
                {
                   // failed to download
                }
            }];

            [completionOperation addDependency:operation];
        }
    }

    [queue addOperations:completionOperation.dependencies waitUntilFinished:NO];
    [queue addOperation:completionOperation];

    return YES;
}

But when we are using this code to download, the app crash due to memory problem.(even putting @autoreleasepool did not solve the crashing problem). It seems downloading one-by-one (or even 4 images parallel)is not a suitable option for this amount of data since it cause us memory problem.I just want to know what is the best way to download this amount of images from server to the iPad?

Thanks in advance...

Vizllx
  • 9,135
  • 1
  • 41
  • 79
Jun
  • 21
  • 2
  • try to use : https://developer.apple.com/library/ios/samplecode/LazyTableImages/Introduction/Intro.html it is provided by apple to download image asynchronously – Malav Soni May 08 '15 at 09:19
  • http://stackoverflow.com/questions/13996621/downloading-multiple-files-in-batches-in-ios-- this will surely help – Vizllx May 08 '15 at 09:19

0 Answers0