0

The following code is within the cellForRowAtIndexPath. I need to edit this code to the following situations;

1.) If there was a problem to download the image this block should return a setFailedBlock block, How can i add it to my code ?

2.) While the image is downloading the user changes the view, then i want to stop executing this code (Stop the download). I think i should write the cancel the block in the viewdiddissapear or viewwilldissapear methods. But i don't know how to write the code to cancel the download. Can someone show me how to do this ?

(note: this block is inside the cellForRowAtIndexPath method so have to access it from viewdiddissapear or viewwilldissapear)

dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//this will start the image loading in bg
dispatch_async(concurrentQueue, ^{        
    NSData *someimageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:someimageURL]];        
    dispatch_async(dispatch_get_main_queue(), ^{
       [cell.imageviewofsomeimage setImage:[UIImage imageWithData:someimageData ] ];
    });
});
Community
  • 1
  • 1
Illep
  • 16,375
  • 46
  • 171
  • 302

1 Answers1

1

if you use NSURLConnection, this class has cancel

AsyncURLConnection discussion

Community
  • 1
  • 1
Volodymyr B.
  • 3,369
  • 2
  • 30
  • 48
  • Isn't there a solution to do it with blocks it self. as in to set a `setFailedBlock ` ? – Illep Feb 06 '12 at 16:16
  • @Illep: Blocks are not a magic bullet. Use them where appropriate. At first sight this does not look like an appropriate place because the block friendly methods cannot be canceled. – JeremyP Feb 06 '12 at 16:35
  • @JeremyP Ok, then what is the correct way of doing it ? the suitable workaround – Illep Feb 06 '12 at 16:39
  • @Illep: if you want to be able to cancel a download, it looks like you need to instantiate an `NSURLConnection`, set a delegate and implement the delegate methods. https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html#//apple_ref/doc/uid/20001836-170129 – JeremyP Feb 06 '12 at 16:43
  • I am also using `ASIRequest` So is there a way to do it using `ASIRequest` ? – Illep Feb 06 '12 at 16:48