释放GCD调度队列属性的正确方法是什么? [英] What is the correct way to release a GCD dispatch queue property?

查看:189
本文介绍了释放GCD调度队列属性的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用通过其所有者的属性访问的dispatch_queue,如下所示:

@property (nonatomic, assign) dispatch_queue_t queue;

请注意assign关键字.队列在对象的整个生命周期中都被使用,因此归对象所有.当拥有对象被释放时,我释放队列:

-(void)dealloc
{
    dispatch_release(self.queue);
    self.queue = nil;
}

如何正确发布此内容?使用retain/release可以工作吗?

如果调用release时队列中有待处理/正在运行的东西会发生什么?

解决方案

以下内容是从开发人员文档中窃取的:

调度队列和其他调度 对象是参考计数数据 类型.创建序列号时 调度队列,它有一个初始 参考计数为1.您可以使用 dispatch_retain和dispatch_release 增加和减少的功能 该引用根据需要进行计数.什么时候 队列的引用计数达到 零,系统异步 取消分配队列.

当您的应用程序不再需要时 调度队列,它应该释放 它与dispatch_release函数. 提交给 队列持有对该队列的引用, 因此队列直到 所有待处理的区块均已完成.

注意:您无需保留或 释放任何全球调度 队列,包括并发 调度队列或主调度 队列.任何试图保留或 释放队列将被忽略.

因此,在任何您要使用-retain的地方都应该使用dispatch_retain,在任何要使用-release的地方都要使用dispatch_release.

调度队列遵循与Objective-C对象相同的常规内存管理约定.并且直到所有排队的块都完成后,它们才会被取消分配.

如果您确实想要关闭调度队列的方法:无法通过任何类型的API取消所有排队的块,因此它们必须始终运行到完成状态.加快此过程的一种方法是在管理调度队列的类中有一个BOOL变量:_isValid.当您要关闭队列时,可以将_isValid设置为NO.提交到队列的所有块都应先检查_isValid,然后再执行任何工作.

旁注:使用NSOperationQueue可能更合适.参见克里斯·汉森(Chris Hanson)的博客文章.

I'm using a dispatch_queue which is accessed through a property of its owner, like this:

@property (nonatomic, assign) dispatch_queue_t queue;

Note the assign keyword. The queue is used throughout the objects life and thus owned by the object. I release the queue when the owning object is deallocated:

-(void)dealloc
{
    dispatch_release(self.queue);
    self.queue = nil;
}

How do I properly release this? Would using retain/release work?

What happens if there is stuff pending/running on the queue while calling release?

解决方案

The following is stolen from the developer documentation:

Dispatch queues and other dispatch objects are reference-counted data types. When you create a serial dispatch queue, it has an initial reference count of 1. You can use the dispatch_retain and dispatch_release functions to increment and decrement that reference count as needed. When the reference count of a queue reaches zero, the system asynchronously deallocates the queue.

When your application no longer needs the dispatch queue, it should release it with the dispatch_release function. Any pending blocks submitted to a queue hold a reference to that queue, so the queue is not deallocated until all pending blocks have completed.

Note: You do not need to retain or release any of the global dispatch queues, including the concurrent dispatch queues or the main dispatch queue. Any attempts to retain or release the queues are ignored.

So anywhere you would use -retain use dispatch_retain and anywhere you would use -release use dispatch_release.

Dispatch queues follow the same general memory management conventions as objective-c objects. And they won't be dealloc'ed until all blocks queued are finished.

If you do want a way to shut down a dispatch queue: There's no way to cancel all enqueued blocks via any sort of API, so they always must run to completion. One way to expedite this process is to have a BOOL variable in the class managing the dispatch queue: _isValid. When you want to shut down the queue, you can set _isValid to NO. All blocks submitted to the queue should first check _isValid before doing any work.

A side comment: It may be more appropriate to use NSOperationQueue. See Chris Hanson's blog post.

这篇关于释放GCD调度队列属性的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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