Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C: Execute block or function in specific NSThread

NSObject has a method -performSelector:onThread:withObject:waitUntilDone: that allows executing any method on the calling object in a specific NSThread.

Is there a similar and convenient way to execute a C-style function or block in a specific NSThread?

like image 319
Bri Bri Avatar asked Aug 23 '26 22:08

Bri Bri


1 Answers

The NSThread API is largely selector-based. In iOS 10 and macOS 10.12, there is block-based API for the initializing or detaching of the thread, but not for performing some task on an existing NSThread. If you want to use block-based API, you'd generally use dispatch or operation queues, which gets you out of the weeds of managing NSThread instances yourself.

But if you google "NSThread block extension", you'll see there are simple third-party APIs to provide block methods for NSThread. E.g., GTMNSThread+Blocks.h or NSThread-MCSMAdditions.

like image 140
Rob Avatar answered Aug 25 '26 12:08

Rob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!