Search code examples
iosnsoperationqueuecancellation

NsoperationQueue Cancel all operations is not cancelled until it finishes the operation


In my view i have image view, the data for image view comes form Url, the images are around 1-3 MB . If the user Swipes then i want to load the next image, Every thing works fine if swiped slowly, But when i swiped Fastly I want to Cancel the previous operation and start with new url.

For Ex. if user swipes 4 times if the operations for 2nd and 3rd images is in the middle, i want to cancel those and start downloading 4 th image

But now In the place of 4th image I’m getting first 2nd image follows 3rd and then 4 th image appears.

Here is my sample code

- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)aSwipeGestureRecognizer {


    [BackgroundOperation cancelAllOperations];  // To cancel previous one 


    [self performSelector:@selector(LoadImage) withObject:nil afterDelay:0.1];


}

-(void)LoadImage
{
    BackgroundOperation=[[NSOperationQueue alloc]init];  


    imgvww.image=[UIImage imageNamed:@"loader.png"]; // Place holder    till download finishes 


   [BackgroundOperation addOperationWithBlock:^
     {
         UIImage *img=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[self.ItemDetails objectAtIndex:0] objectForKey:@"ImageUrl"]]]];  // Getting data from URL 

         [[NSOperationQueue mainQueue] addOperationWithBlock:^{


             imgvww.image=img;  //Adding to image view after completion 


         }];

     }];
 }

Thank you.


Solution

  • Canceling an operation merely sets its isCancelled Flag to true.

    You're responsible for checking to see if your operation has been cancelled, before it starts to run (or while it is running, if it is a long-running operation).

    You can check if your operation is cancelled within an operation block but I'd recommend subclassing, instead of using a block.