Running an anonymous block in a specific background thread

At first glance, this seemed like a simple question, but I just can’t understand how to start an anonymous block in a specific background thread, that is, I am looking for equivalents of -performSelector:onThread:withObject:waitUntilDone: .

Related: Is it possible to associate the send queue with a specific background thread, just like the main queue is associated with the main thread of the application?

Edit Clarified what I'm looking for to run an anonymous block

+8
objective-c cocoa-touch cocoa grand-central-dispatch
source share
3 answers

I recently saw this RunOnThread() function in Mike Ash PLBlocksPlayground (zip file, see BlocksAdditions.m):

 void RunOnThread(NSThread *thread, BOOL wait, BasicBlock block) { [[[block copy] autorelease] performSelector: @selector(my_callBlock) onThread: thread withObject: nil waitUntilDone: wait]; } 

That's what I was looking for.

There are tons of other very useful utility-related blocks in PLBlocksPlayground, most of which Mr. Ash explains in this post .

+6
source share

If I understand correctly, you should do this:

 dispatch_queue_t thread = dispatch_queue_create("your dispatch name", NULL); dispatch_async(analyze, ^{ //code of your anonymous block }); dispatch_release(thread); 

You can also write some method that will block it, but you need to know what type of parameters it will store:

 -(void)performBlock:(void (^)(SomeType par1, SomeType par2))block ToData:(Sometype)data; 

You can call it with an anonymous block:

 [something performBlock:^(SomeType par1, SomeType par2){ //do your stuff } ToData: data] 

And in the method, you can call your block as a simple C function:

 block(par1, par2); 
+7
source share

Block is a function. Call it what you would call any other function.

-one
source share

All Articles