1

In ios 6, I have an image downloader method that calls sendAsynchronousRequest. When number of images are too many completion blocks are never called and there are over 60 worker threads that are never destroyed(so the image downloading is never completed). In ios 7 it seems to be working fine. How do you make this work? code is as below

for(NSDictionary * imageDict in thumbnailJSONArray){
    NSString * thumbNailString = [imageDict objectForKey:JSON_URL];
    //other codes..
    [self downLoadThumbNails:numbertoAdd urlRequest:thumbNailString];

}
-(void)downLoadThumbNails:(NSUInteger) numberToAdd urlRequest:(NSString*)urlString {
    if(self.imageQueue == nil){
        self.imageQueue = [[NSOperationQueue alloc] init];
    }  

    [NSURLConnection sendAsynchronousRequest:urlRequest queue:self.imageQueue completionHandler:^(NSURLResponse *response, NSData* data, NSError *connectionError) {
    //image setting codes
    }];
}

--EDIT-- To be more clear, image setting codes are never called, so the downloaded images are never set.

andylee
  • 255
  • 2
  • 6
  • Yes, you can create some custom NSOperation-s and add them to one or more NSOperationQueue-s, it will be right way. – Sergei Nikitin Apr 10 '14 at 09:53
  • Can you explain why my code is not working? So this is just a bug in sendAsynchronousRequest and I should use sendSynchronousRequest and manage threads/operations/queues myself? – andylee Apr 10 '14 at 09:57
  • Could you be more specific about what is not working? – Łukasz Tomaszewski Apr 14 '14 at 08:53
  • @ŁukaszTomaszewski simply put, image setting codes are never called. – andylee Apr 15 '14 at 07:44
  • 1
    The operation queue you are passing sendAsynchronousRequest is only the queue that the connection's delegate callbacks will be executed on. NSOperationQueues are concurrent by default. For what you are trying to do, try setting maxConcurrentOperationCount to 1 to make it a serial queue. This will prevent you from spawning a large number of threads at a time. – quellish Apr 15 '14 at 07:54

0 Answers0