3

Apple's doc我无法找到当我想捕获CoreFoundation对象时我能做些什么。是否可以阻止捕获CoreFundation对象?

但在苹果的Concurrency Programming Guide。这似乎示例代码使用一些代码,派遣对象不支持ARC就像这样:

void average_async(int *data, size_t len, dispatch_queue_t queue, void (^block)(int)) 

    { 

     // Retain the queue provided by the user to make 

     // sure it does not disappear before the completion 

     // block can be called. 

     dispatch_retain(queue); 


     // Do the work on the default concurrent queue and then 

     // call the user-provided block with the results. 

     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

      int avg = average(data, len); 

      dispatch_async(queue, ^{ block(avg);}); 



      // Release the user-provided queue when done 

      dispatch_release(queue); 

     }); 
    } 

我需要之前,使用CFObjectDispatchObject。但是如果我需要多次调用该块?

也许我可以使用__attribute__((NSObject)),但我不知道会发生什么!

苹果是否对此有所说?

回答

1

我没有看到任何明确的苹果,但我确实看到一些mentions in the llvm.org documentation,我发现它在in this cocoa-dev mailing list thread详细阐述。

看起来你应该使用__attribute__((NSObject))是好的,因为它给了一个隐含的“__strong”资格(从文件),并在实际意义上(从邮件列表线程),对象保留当块排队并在块完成时发布。

+0

我也有帖子[一个问题](http://stackoverflow.com/questions/26090776/right-way-to-use-attribute-nsobject-with-arc))有关如何使用__attribute __((NSObject ))。当使用这个时,似乎事情变得非常奇怪。 – Karl 2014-09-29 01:13:37

1

首先,dispatch_queue_t不是核心基础对象。

调度对象被编译器视为Objective-C对象(对于ARC和块目的)if your deployment target is iOS 6+/OS X 10.8+

+0

我不说dispatch_queue是一个Core Fundation对象。我只是觉得它必须像iOS6之前的Core Fundation对象那样管理它的内存。所以我想知道如何管理Core Fundation对象,因为我看不到任何形式的苹果文档。 – Karl 2014-09-29 09:08:44

相关问题