Bri Bri
Bri Bri

Reputation: 2270

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?

Upvotes: 3

Views: 1124

Answers (1)

Rob
Rob

Reputation: 437442

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.

Upvotes: 3

Related Questions