简体   繁体   English

如何在iOS中取消后台阻止操作?

[英]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.

相关问题 如何取消或暂停MKNetworkKit iOS的下载操作? - How to cancel or pause download operation of MKNetworkKit iOS? 在特定时间后如何取消iOS本地通知? - How do I cancel an iOS local notification after a particular time? 如何在 iOS 警报中显示不同的取消颜色? - How do I show different color for cancel in iOS alert? 如何取消在后台运行的线程? (iOS) - How to cancel a thread running in the background ? (iOS) 如何在iOS中取消后台的所有NSURLConnections - How to Cancel all NSURLConnections in Background in iOS iOS - 使用“设置”和“取消”按钮打开“位置服务” - “如何捕获取消”按钮单击 - iOS - Turn on Location Services with Settings and Cancel buttons -How do I Capture Cancel button click iOS:强制取消NSOperationQueue中的操作 - iOS: Force to cancel Operation in NSOperationQueue 如何在Realm(iOS)中进行写入操作同步? - How do I make a write operation in Realm (iOS) synchronous? 如何在iOS中以编程方式取消或阻止来电?可能吗? - How to cancel or block incoming calls programmatically in iOS? Is it possible? ios驱动器SDK | 如何检查每张票,然后将其取消? 例如,如果它的标签= 1,我需要取消它 - ios drive sdk | How do I check each ticket, then cancel it? e.g. if it has tag = 1, I need to cancel it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM