杀死iOS中dispatch_async队列中的项目 [英] kill items in a dispatch_async queue in iOS

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

问题描述

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

  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上释放它,则调用它在他们仍然继续运行。

解决方案

调度队列中没有明确规定终止。为此,测试转义位置以确定终止是有点常见的。基本上,它是一个信号量。



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

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



最后,即使是NSOperation的实现也需要进行测试以便在一致的位置退出(因为只是杀死一个线程可能会导致在分配/重新训练中操作的数据不一致。


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];
        }
    });

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

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.

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队列中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆