Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blocks in Obj-C for performSelector: onThread:?

While it wouldn't be hard to spin up my own method for this, it wouldn't be as efficient as

- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait;

is there a built-in method to do this with blocks?

If not, is there a way I can avoid creating an object, putting the block on it, and passing it to a selector method? I also wonder if there would be problems with data accessed within the block...

like image 905
Dan Rosenstark Avatar asked Mar 25 '26 01:03

Dan Rosenstark


1 Answers

The blocks equivalent to the performSelector: method is CFRunLoopPerformBlock — you just need to get a reference to that thread's CFRunLoop.

Have a look at Apple's documentation for the function and note the caveat in the discussion — you'll probably want to call CFRunLoopWakeUp afterwards so the block is executed right away.

like image 191
Nick Hutchinson Avatar answered Mar 26 '26 16:03

Nick Hutchinson