NsoperationQueue Canceling all operations is not canceled until the operation is completed

In my opinion, I have an image representation, the data for viewing images comes from Url, the images are about 1-3 MB. If the user is Swipes, then I want to download the following image. Each thing works fine if you scroll slowly, but when I quickly checked, I want to undo the previous operation and start with a new URL.

For example. if the user commits 4 times, if the operations for the 2nd and 3rd images are in the middle, I want to cancel them and start downloading the 4th image

But now, instead of the 4th image, Im, the first first image follows the 3rd, and then the 4th 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 }]; }]; } 

Thanks.

+7
ios nsoperationqueue cancellation
source share
3 answers

Canceling an operation simply sets the isCancelled flag to true.

You are responsible for checking whether your operation was canceled before it starts (or while it is running, if it is a lengthy operation).

You can check if your operation is canceled in the work block , but I would recommend a subclass instead of using the block.

+5
source share

Calling cancelAllOperations on an NSOperationQueue will simply call cancel for each of its operations. If NSOperation does not cancel cancel , then it will never be able to cancel.

There is no concept of canceling NSBlockOperation after its launch. The block just executes, and here's the thing.

If you want to specify special undo behavior (for example, undo image loading) you need to subclass NSOperation and override cancel .

There are many examples of this in AFNetworking or SDWebImage

To cancel image loading, you need to wrap NSURLSesionDownloadTask in NSOperation and then override cancel to call cancel on NSURLSesionDownloadTask

+4
source share

Canceling an operation will update its isCancelled property to YES .
To cancel the operation, you must do the following:

 NSBlockOperation * op = [NSBlockOperation new]; __weak NSBlockOperation * weakOp = op; // Use a weak reference to avoid a retain cycle [op addExecutionBlock:^{ // Put this code between whenever you want to allow an operation to cancel // For example: Inside a loop, before a large calculation, before saving/updating data or UI, etc. if (weakOp.isCancelled) return; // Do something.. ]; 
+3
source share

All Articles