Reputation: 2973
With Cocoa, how can I schedule an operation (block of code) to run asynchronously on a background thread?
There is a risk that the operation will block for a long time, so it is of critical importance that the operation is not executed on the main thread.
Of course, I can create my own thread (NSThread
), but it seems to me that Cocoa should offer an easier/better way.
Upvotes: 1
Views: 653
Reputation: 2062
You can use the Grand Central Dispatch command dispatch_async() to easily run code in the background. An example of this:
dispatch_queue_t bgQueue = dispatch_queue_create("bgQueue", NULL);
dispatch_async(bgQueue, ^{
//your code here
});
Upvotes: 1