Best way to do selectoronmainthread in c lens?

I am writing a client-server-application for iPhone. And I have a question about slicing. When I access my online database from the device, I need to do this in a separate thread so as not to freeze the UI / main thread. But when I respond to the data received from the database, I call this method in the main thread: performSelectorOnMainThread. The fact is that this only allows me to send one argument / object to a method (WithObject), sometimes I have more arguments that I want to pass. and one more thing is that I have to pass this one object. I can not get through nil if I attach the application.

This is my code today .. and I'm worried that I use methods and thread incorrectly.

- (IBAction)testServerAction:(id)sender { [self.imageView setHidden:YES]; [self.activityView setHidden:NO]; [self.activityView startAnimating]; dispatch_queue_t testServer = dispatch_queue_create("Test-Server-Thread", NULL); dispatch_async(testServer, ^{ if ([self.arrayWithServerConnections count] > 0) { NSString *messageToShow; if ([self testServerMethod]) { messageToShow = @"Server is working!"; [self performSelectorOnMainThread:@selector(showMessageBoxWithString:) withObject:messageToShow waitUntilDone:YES]; [self performSelectorOnMainThread:@selector(threadedUIActivityRemover:) withObject:nil waitUntilDone:YES]; }else { messageToShow = @"Server is NOT working!"; [self performSelectorOnMainThread:@selector(showMessageBoxWithString:) withObject:messageToShow waitUntilDone:YES]; [self performSelectorOnMainThread:@selector(threadedUIActivityRemover:) withObject:nil waitUntilDone:YES]; } } }); dispatch_release(testServer); } -(void)threadedUIActivityRemover:(NSString *)string { [self.imageView setHidden:NO]; [self.activityView setHidden:YES]; [self.activityView stopAnimating]; } 

This is a good way to do this, is there anything other than performing a SelectorOnMainThread that you can point me to what works best?

As you can see, I am passing nil to the NSString argument in this example because I need to pass something, if I don't have NSString as an argument in the method, the application resets evan when passing nil .. Why is this? .. please do it's a little clearer for me!

//Thanks!

+7
source share
4 answers

Well, you are already using dispatch_async . Then you should just use

  dispatch_async(dispatch_get_main_queue(),^ { ... } ); 

from inside the background thread to perform actions in the main thread. For example,

  if ([self testServerMethod]) { dispatch_async(dispatch_get_main_queue(),^ { [self showMessageBoxWithString: @"Server is working!"]; [self threadedUIActivityRemover:nil]; } ); }else ... 

It has no limit on the number of arguments for the methods you call.

+10
source

Pass a collection, such as a dictionary of unarchived objects.

+2
source

You can also use NSInvocation .

+1
source

since you are passing instance variables, another option would be to pass self and create a self-flow.

0
source

All Articles