8

I am working on a task (iOS5 + only) that involves downloading thousands of images from the server. The images belong to certain categories and each category can have hundreds of images. What I need to do is this :-

1) Make sure the app downloads any missing images in background if the app is active (even when the user is browsing some other areas of the app that are not related to photos).

2) When the user clicks on a Photo Category, the images in that Category must be downloaded as high priority because those are the ones that need to be visible immediately.

All of the above happens only if the image is not already available offline. Once it's downloaded, the image will be used from local storage.

To solve this, the logic I am using is this :-

1) In AppDelegate.m, in applicationDidBecomeActive, I start downlading any missing images. To do this, I make a Core Data query, find out which images are missing, and start downloading them in a thread with BACKGROUND priority. Something like this :-

 dispatch_queue_t imageDownloadQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
dispatch_async(imageDownloadQueue, ^{
    [DataDownloader downloadMissingImages];
});
dispatch_release(imageDownloadQueue);

The downloadMissingImages code looks like this :-

NSOperationQueue *downloadQueue = [[NSOperationQueue alloc] init];
        downloadQueue.maxConcurrentOperationCount = 20;

        for(MyImage *img in matches)
        {
            NSURLRequest *request = [NSURLRequest requestWithURL:img.photoUrl];
            AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request success:^(UIImage *image) {

                [MyImage imageFromAPI:image inManagedObjectContext:document.managedObjectContext];

                NSLog(@"Successfully downloaded image for %@", img.title);      
            }];

            [downloadQueue addOperation:operation];
        }

This works, but it blocks the main UI and the app crashes after a while. This is when I try to download about 700 images. With more images, it would certainly crash.

2) When a user clicks on a category, I need to download those images first as they must be shown to the user immediately. I am not sure how I can interrupt the missingImages call and tell it to start downloading certain images before others.

So, basically, I need to download all the missing images in the background but if the user is browsing photo category, those images must take high priority in the download queue.

I am at a loss how to get this working efficiently. Any thoughts?

The crash logs look like this

PAPP(36373,0xb065f000) malloc: *** mmap(size=16777216) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
PAPP(36373,0xb065f000) malloc: *** mmap(size=16777216) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
Jun 24 11:39:45 MacBook-Pro.local PAPP[36373] <Error>: ImageIO: JPEG    Insufficient memory (case 4)

Thanks in advance.

Anuj Gakhar
  • 681
  • 2
  • 13
  • 26

1 Answers1

3

About the crash, I guess that your app is killed due to either of two options:

  1. the app becoming unresponsive (and thus not responding to the iOS sentinel process);

  2. too much memory used in the loop creating over 700 request operations.

To clarify what is really happening, you should provide more info about the crash (the console log). In any case, the fix would be loading the images in chunks of maybe 10 or 20 each (you could go even 1 by 1, if you like, I don't see much problem with that).

About the second point, what about this:

  1. download a higher priority image in the main thread (via an async download, of course, to avoid blocking the UI);

  2. before starting downloading an "offline" image, you check whether the image has been already downloaded in the meanwhile through a "higher-priority" download.

To handle point 2 well, you would probably need to queue your custom operation instead of an AFImageRequestOperation in order to do the check before the actual download.

EDIT:

About downloading images in chunk, what you could do is using dispatch groups to group your networking operations:

dispatch_group_t group = dispatch_group_create();

<your_core_data_query>

for (...) {
    dispatch_group_enter(group);

        NSURLRequest *request = [NSURLRequest requestWithURL:img.photoUrl];
        AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request success:^(UIImage *image) {
            [MyImage imageFromAPI:image inManagedObjectContext:document.managedObjectContext];
            NSLog(@"Successfully downloaded image for %@", img.title);

            dispatch_group_leave(group);     //<== NOTICE THIS
        }];

}

dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
dispatch_release(group);

In this sample, I am using a dispatch group to group a number of async operations and wait for them being executed all; when dispatch_group_wait returns, you can execute another round of that (querying core data then dispatching ops).

About your other question (how do I check whether a higher priority queue has already downloaded a certain image), you should do a core data query before executing each AFImageRequestOperation; one possibility is deriving your own class of it and overriding the start method to do the check.

On both accounts, you could simplify much the logics of all this by downloading the images one at the time (i.e., the for (...) loop would not be there; you simply query the next image to download and download it; before downloading you check it is not already there.

I would suggest going this easier path.

Hope it helps.

sergio
  • 68,819
  • 11
  • 102
  • 123
  • Hi Sergio, have you got any code sample to demonstrate chunked downloads like you suggest? – Anuj Gakhar Jun 24 '12 at 10:37
  • Also, how do I check whether a higher priority queue has already downloaded a certain image? Any code samples for that? – Anuj Gakhar Jun 24 '12 at 10:38
  • Have modified the question to include crash logs - I generate thumbnails from each image download..so I guess 700 images is definitely causing the memory leak here.. – Anuj Gakhar Jun 24 '12 at 10:45
  • thanks for the code...I notice AFHTTpClient has enqueueBatchOfHTTPRequestOperations which also uses dispatch groups so maybe I could use that...will this code run in the background without blocking the UI..currently it seems to block the UI.. – Anuj Gakhar Jun 24 '12 at 13:24
  • as far as I understand, you are executing all this in `dispatch_async(imageDownloadQueue,`, so UI should be not blocked... – sergio Jun 24 '12 at 13:28
  • I am using dispatch_async but while the download is in progress, the UI is blocked..cant click anywhere – Anuj Gakhar Jun 24 '12 at 13:30
  • are you using the "chunked" version or your original code? in any case, I do not understand well... are you sure the networking operation is blocking the UI (main thread) or the background operations are hogging the processors (like in: too much processing, so the UI gets less time?). Could it be the core data op that is blocking the UI? perhaps you could try reducing maxConcurrentOperationCount to 1 to see if things change... – sergio Jun 24 '12 at 13:39
  • I am trying to do as follows:- 1) In the Grid View in the reusable cell, I download the image for that cell as and when the cell comes in view. This is handled by GMGridView, so I don't do much here except checking if the image for that cell does not exist, then download it. 2) Then in the AppDelegate, I am trying to run a background process that will silently download all images (so the user does not have to click and scroll to each photo to download it). This background process is fired off using dispatch_async and downloads the missing images. This is what blocks the UI. – Anuj Gakhar Jun 24 '12 at 13:49
  • And I tried maxConcurrentOperationCount = 1 and also tried chunked downloads (only adding 20 operations to the enqueueBatchOfHTTPRequestOperations at a time) and then wait for it to finish. Ui still blocked. This is not a simulator issue as it blocks the UI on the device too. I am beginning to think this is a case of processors being hogged... – Anuj Gakhar Jun 24 '12 at 13:52
  • @sergio Would you also use the `dispatch_group`and `dispatch_group_wait` calls if you download the image one at the time? – d.ennis Aug 22 '13 at 11:01
  • @d.ennis: dispatch groups are useful when you need a way to handle a number of tasks as a single unit and synchronize some other task on that group of tasks being completed; if you just download the images one at a time, I don't see any need for a dispatch group. If you execute, say, 30 simultaneous image downloads and you need a way to know when all those tasks complete, a group is useful. hope this helps. – sergio Aug 25 '13 at 09:39