0

After a lot of reading and recommendations, I am trying to implement SDWebImage framework in my app. I am able to download images from the server but the issue is that as the images are different sizes, they are not downloaded in the order that I call them (and would like them to be).

Here is the framework for reference: https://github.com/rs/SDWebImage

As you can see evidence of this from the print-line:

2013-08-20 11:11:01.785 Project[10785:c07] SD LOAD 1 array count = 1
2013-08-20 11:11:02.668 Project[10785:c07] SD LOAD 0 array count = 2
2013-08-20 11:11:02.946 Project[10785:c07] SD LOAD 3 array count = 3
2013-08-20 11:11:03.306 Project[10785:c07] SD LOAD 2 array count = 4
2013-08-20 11:11:03.862 Project[10785:c07] SD LOAD 4 array count = 5
2013-08-20 11:11:04.094 Project[10785:c07] SD LOAD 5 array count = 6
2013-08-20 11:11:04.793 Project[10785:c07] SD LOAD 6 array count = 7
2013-08-20 11:11:04.837 Project[10785:c07] SD LOAD 7 array count = 8
2013-08-20 11:11:05.046 Project[10785:c07] SD LOAD 8 array count = 9
2013-08-20 11:11:05.428 Project[10785:c07] SD LOAD 9 array count = 10

Below is my method:

-(void)sdWebLoadImages:(NSMutableArray*)imagesURLS{

    [_loadedImages removeAllObjects];
    NSMutableArray *tempArray = [[NSMutableArray alloc]init];
    _loadedImages = tempArray;
    for (int i=0;i<imagesURLS.count;i++){

    [_manager downloadWithURL:[imagesURLS objectAtIndex:i]
                     options:0
                    progress:^(NSUInteger receivedSize, long long expectedSize)
     {
         // progression tracking code
     }
                   completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType,BOOL finished)
     {
         if (image && finished)
         {

             [_loadedImages addObject:image];
            NSLog(@"SD LOAD %i array count = %i",i,_loadedImages.count);
         }
     }];
    }
}

How can I fix this (is it something in my for loop logic?). Also, I am trying to cancel all the downloads when a button is clicked, and am using [_manager cancelAll] but doesn't seem to work - any idea why?

Thanks fam!

ADDED:

The issue now is that the manager inserts all the cached images into the array first, causing the order to be disrupted, even after I set the setMaxConcurrentOperationCount to 1. Not sure how to keep the downloading in order even with cached images.

royherma
  • 4,095
  • 1
  • 31
  • 42
  • 2
    I'm not familiar with this framework. If it's using NSOperationQueue behind the scenes, can you set it so that maxOperations = 1? That way it won't run a bunch of parallel operations but will do them in the order you put them in the queue. – Daddy Aug 20 '13 at 18:19
  • the [_manager downloadWithUrl...] returns an operation which i guess I can add to my own operationQ but it crashes when I try and do that so I'm kinda avoiding it at this time... – royherma Aug 20 '13 at 18:27

1 Answers1

2

As simple as ice and as pointed by 'Justin Amberson':

Go to SDWebImageDownloader.m

I am assuming you are using the latest SDWebImage library.

On line 72:

_downloadQueue.maxConcurrentOperationCount = NSOperationQueueDefaultMaxConcurrentOperationCount;

Change it to:

_downloadQueue.maxConcurrentOperationCount = 1;

It will download one image at a time.

Cheers

Reno Jones
  • 1,979
  • 1
  • 18
  • 30
  • Thanks Reno! Also, would all i need to is call [_manager cancelAll] to cancel the image loading for all of those requests? – royherma Aug 20 '13 at 18:34
  • Yes indeed: [[SDWebImageManager sharedManager] cancelAll] :) – Reno Jones Aug 20 '13 at 18:36
  • Looks like an opportunity to expose that property to the outside. – Daddy Aug 20 '13 at 18:46
  • there is still a problem. the download manager will load the cached images before the ones it needs to download and insert them into the array...messing up there order again! – royherma Aug 20 '13 at 22:15