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!
Objective coat
source share