NsoperationQueue 取消所有操作直到完成操作才取消
NsoperationQueue Cancel all operations is not cancelled until it finishes the operation
在我看来,我有图像视图,图像视图的数据来自 Url,图像大约为 1-3 MB。
如果用户滑动,那么我想加载下一张图片,如果滑动缓慢,一切正常,但是当我快速滑动时,我想取消之前的操作并从新的 url.
开始
例如。如果用户滑动 4 次,如果第二张和第三张图片的操作在中间,我想取消这些并开始下载第四张图片
但是现在在第 4 张图片的位置,我首先看到第 2 张图片,然后是第 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
}];
}];
}
谢谢。
在 NSOperationQueue
上调用 cancelAllOperations
只会在其每个操作上调用 cancel
。如果 NSOperation
没有覆盖 cancel
那么它永远不会被取消。
NSBlockOperation
没有一旦开始就取消的概念。该块只是执行,就是这样。
如果你想指定特殊的取消行为(比如取消你的图片下载),你需要继承 NSOperation
并重写 cancel
。
在AFNetworking
或SDWebImage
中有很多这样的例子
要取消图像下载,您需要将 NSURLSesionDownloadTask
包裹在 NSOperation
中,然后覆盖 cancel
以在 NSURLSesionDownloadTask
上调用 cancel
取消操作只是将其 isCancelled
标志设置为真。
您有责任检查您的操作是否已被取消,在它开始 运行 之前(或者当它是 运行ning 时,如果它是 long-运行宁操作).
你可以 check if your operation is cancelled within an operation block 但我建议子类化,而不是使用块。
取消操作只会将其 isCancelled
属性 更新为 YES
。
要取消操作,您应该执行以下操作:
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..
];
在我看来,我有图像视图,图像视图的数据来自 Url,图像大约为 1-3 MB。 如果用户滑动,那么我想加载下一张图片,如果滑动缓慢,一切正常,但是当我快速滑动时,我想取消之前的操作并从新的 url.
开始例如。如果用户滑动 4 次,如果第二张和第三张图片的操作在中间,我想取消这些并开始下载第四张图片
但是现在在第 4 张图片的位置,我首先看到第 2 张图片,然后是第 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
}];
}];
}
谢谢。
在 NSOperationQueue
上调用 cancelAllOperations
只会在其每个操作上调用 cancel
。如果 NSOperation
没有覆盖 cancel
那么它永远不会被取消。
NSBlockOperation
没有一旦开始就取消的概念。该块只是执行,就是这样。
如果你想指定特殊的取消行为(比如取消你的图片下载),你需要继承 NSOperation
并重写 cancel
。
在AFNetworking
或SDWebImage
要取消图像下载,您需要将 NSURLSesionDownloadTask
包裹在 NSOperation
中,然后覆盖 cancel
以在 NSURLSesionDownloadTask
上调用 cancel
取消操作只是将其 isCancelled
标志设置为真。
您有责任检查您的操作是否已被取消,在它开始 运行 之前(或者当它是 运行ning 时,如果它是 long-运行宁操作).
你可以 check if your operation is cancelled within an operation block 但我建议子类化,而不是使用块。
取消操作只会将其 isCancelled
属性 更新为 YES
。
要取消操作,您应该执行以下操作:
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..
];