在 iOS 中杀死 dispatch_async 队列中的项目

kill items in a dispatch_async queue in iOS(在 iOS 中杀死 dispatch_async 队列中的项目)

本文介绍了在 iOS 中杀死 dispatch_async 队列中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 dispatch_async 在后台运行一堆项目,有时我想杀死队列中的内容 - 这可能吗?例如,此代码在视图上运行,然后用户返回屏幕.无论返回导航如何,所有这些触发的操作都会继续运行.理想情况下,我想阻止这些项目运行:

I am running a bunch of items in the background using dispatch_async and sometimes I want to kill what I have in the queue - is this possible? For instance this code is run on a view, and then the user goes back a screen. All of these fired actions keep running regardless of the back navigation. Ideally I would like to kill these items from running:

dispatch_async(dispatch_get_global_queue(2, 0), ^{
        for (int i=0; i<[self.manufacturers count]; i++) {
            NSString *manufacturerID = [[[self.manufacturers objectAtIndex:i] ManufacturerID] stringValue];
            [self doSync:manufacturerID withIndex:i setTimer:YES];
        }
    });

如果我创建一个队列并命名它,然后在调用视图的 dealloc 上释放它,它们仍然会继续运行.

If I create a queue and name it and then release it on the dealloc of the view this is called in they still continue to run.

推荐答案

调度队列中没有明确规定终止.为了做到这一点,测试一个逃生位置以确定终止是有点常见的.基本上,这将是一个信号量.

There's no explicit provision in dispatch queues for termination. In order to do this, it is somewhat common to test an escape location to determine termination. Basically, it'd be a semaphore.

NSOperationQueue(更高级别的抽象,但仍然在底层使用 GCD 构建)确实支持取消操作.因此,例如,您可以创建一系列 NSOperations 并将它们添加到 NSOperationQueue,然后在不需要完成时将 -cancelAllOperations 消息发送到队列.

NSOperationQueue (a higher level abstraction, but still build using GCD underneath) does have support for canceling operations. So, for example, you can create a series of NSOperations and add them to an NSOperationQueue and then message -cancelAllOperations to the queue when you don't need it to complete.

您选择的很多架构将取决于其中有多少正在运行以及它们是否具有不同的触发器.在这些实现中,NSOperation 可能是最干净"的解决方案,因为您有一个任意队列,您可以在该队列中观察要完成的操作,还可以取消未完成的操作.进一步降低黑客规模将是一个不稳定的位置,这些块中的每一个都在一个紧密的循环内监视,以确定它们是否会过早完成.再往下是相同基本功能的全局变量.

A lot of the architecture you choose will depend on how many of these are operating and whether they have different triggers. Among the implementations, NSOperation is likely the "cleanest" solution, since you have an arbitrary queue which you can watch for operations to be finished on and you can also cancel outstanding operations. Further down the scale of hack would be a volatile location that each of these blocks watch inside of a tight loop to determine if they're going to finish prematurely. Yet further down would be a global variable for the same basic function.

最后,即使是 NSOperation 的实现也涉及到一个测试,以便在一个一致的位置退出(因为仅仅杀死一个线程可能会导致正在操作的数据或分配/重新训练中的数据不一致).

In the end, even the implementation of NSOperation involves a test in order to exit in a consistent location (since just killing a thread might result in inconsistencies in the data being operated upon or in allocations/retrains).

这篇关于在 iOS 中杀死 dispatch_async 队列中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 iOS 中杀死 dispatch_async 队列中的项目

基础教程推荐