4

In Apple's doc I can't find what I can do when I want to capture a CoreFoundation object.

But in Apple's Concurrency Programming Guide. It seems the sample code use some code when dispatch object is not support ARC just like this:

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

       });
   }

Do I need to use CFObject like the DispatchObject before. But if I need to invoke the block many times?

Maybe I can use __attribute__((NSObject)), but I don't know what will happen!

Does Apple say something about this?

Karl
  • 665
  • 4
  • 19

2 Answers2

2

I didn't see anything at Apple explicitly, but I do see some mentions in the llvm.org documentation, which I found elaborated on in this cocoa-dev mailing list thread.

It looks like you should be okay for using __attribute__((NSObject)), as it's given an implicit "__strong" qualification (from the documentation) and in a practical sense (from the mailing list thread), the object is retained when the block is queued up and released when the block finishes.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • I also have post [a question](http://stackoverflow.com/questions/26090776/right-way-to-use-attribute-nsobject-with-arc)) about how to use the __attribute__((NSObject)).Seems things become very strange when use this. – Karl Sep 29 '14 at 01:13
1

First of all, dispatch_queue_t is not a Core Foundation object.

Dispatch objects are considered Objective-C objects by the compiler (for ARC and blocks purposes) if your deployment target is iOS 6+ / OS X 10.8+.

Community
  • 1
  • 1
newacct
  • 119,665
  • 29
  • 163
  • 224
  • I don't say dispatch_queue is a Core Fundation object. I just think it has to manage it's memory like a Core Fundation object before iOS6. So I wonder how to manage a Core Fundation object when it is captured by a block because I can't see anything form apple's documents. – Karl Sep 29 '14 at 09:08