Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS performSelectorOnMainThread with multiple arguments

I would like to perform a selector on the main thread from another thread, but the selector has multiple arguments, similar to this:

-(void) doSomethingWith:(int) a b:(float)b c:(float)c d:(float)d e:(float)e { //... }

How can I get this working with performSelectorOnMainThread: withObject: waitUntilDone:?

EDIT

I would like to explain why i need this.

I'm working with UIImageViews on the main thread, and I make the calculations for them on another thread. I use a lot of calculations so if i make everything on the main thread, the app lags. I know that UI elements can only be manipulated on the main thread, this is why i would like it to work this way, so the main thread can listen to touch events without lags.

like image 569
McDermott Avatar asked Nov 30 '11 13:11

McDermott


2 Answers

When you're using iOS >= 4, you'd do this instead:

dispatch_async(dispatch_get_main_queue(), ^{     [self doSomething:1 b:2 c:3 d:4 e:5]; }); 

That's like doing waitUntilDone:NO. If you want to wait until the method is finished, use dispatch_sync instead.

like image 187
DarkDust Avatar answered Sep 20 '22 11:09

DarkDust


You'll need to use a NSInvocation

Create the object, set the target, selector and arguments.
Then, use

[ invocationObject performSelectorOnMainThread: @selector( invoke ) withObject: nil, waitUntilDone: NO ]; 
like image 45
Macmade Avatar answered Sep 20 '22 11:09

Macmade