[英]How do i cancel a background block operation in iOS?
I want to know what is the proper way to use [self.operationQueue cancelAllOperations]; 我想知道使用[self.operationQueue cancelAllOperations]的正确方法是什么;
I am using a block operation in async to fetch results from my API. 我在异步中使用块操作从我的API中获取结果。 Sometimes it happens that I get result of first request after second request and those are displayed to user. 有时发生第二个请求之后我得到第一个请求的结果并将其显示给用户的情况。
I am using AFNetworking library for operations. 我正在使用AFNetworking库进行操作。 Any suggestion on how I can make sure that only one request (the latest one) is active at a particular time, and previous one gets cancelled automatically. 关于如何确保在特定时间只有一个请求(最近的一个)处于活动状态,而上一个请求被自动取消的任何建议。
When all operations in a queue are cancelled it is the responsibility of each running operation to stop itself. 取消队列中的所有操作后,每个正在运行的操作都有责任自行停止。 The queue will only prevent future operations from starting. 该队列只会阻止以后的操作开始。 With block operations there isn't really any way to stop as the block doesn't have access to the operation to check if it's cancelled. 使用块操作时,实际上没有任何停止的方法,因为该块无权访问该操作以检查其是否已取消。
It isn't clear exactly what you're using the operation for, but you would need to crate an operation subclass, either to run or to wrap that logic, which at least checked for cancellation before running the final callback to return the result. 尚不清楚您正在使用该操作的确切含义,但您需要创建一个操作子类,以运行或包装该逻辑,该逻辑至少要检查是否已取消,然后再运行最终的回调以返回结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.