ios – NsoperationQueue取消所有操作在完成操作之前不会被取消

在我看来我有图像视图,图像视图的数据来自Url,图像大约1-3 MB.
如果用户Swipes然后我想加载下一个图像,如果我慢慢滑动,每件事情都可以正常工作,但是当我快速滑动时,我想取消之前的操作并从新网址开始.

对于Ex.如果第二和第三张图像的操作位于中间,如果用户滑动4次,我想取消这些并开始下载第4张图像

但是现在在第4张图像的地方我得到第一张第二张图像跟随第3张然后第4张图像出现.

这是我的示例代码

- (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 


         }];

     }];
 }

谢谢.

解决方法

取消操作只是将其isCancelled Flag设置为true.

在开始运行之前(或者在运行期间,如果它是长时间运行的操作),您负责检查操作是否已被取消.

你可以check if your operation is cancelled within an operation block,但我建议继承,而不是使用块.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...