在目标C中执行selectoronmainthread的最佳方法? [英] Best way to performselectoronmainthread in objective c?
问题描述
我正在为iPhone编写客户端服务器应用程序.我有一个关于线程的问题.从设备访问在线数据库时,需要在单独的线程上执行此操作,以免冻结UI/主线程.但是,当响应从数据库中获取的数据时,我在主线程上调用此方法:performSelectorOnMainThread.事实是,这只允许我向方法(WithObject)发送一个参数/对象,有时我想传递更多的参数.另一件事是我必须传递这个对象.如果我崩溃了,我无法通过nil.
I'm writing a client-server app to iPhone. And I have a question about threading. When I access my online database from the device, I need to do this on a separate thread to not freeze the UI/main thread. But when responding to the data I've fetched from the database I call this method on the main thread: performSelectorOnMainThread. The thing is that this only lets me send one argument/object to the method (WithObject), sometimes I have more arguments I want to pass. and another thing about it is that I HAVE TO pass this one object. I can't pass nil, if I do the app crashes.
这是我今天的代码..我担心我使用的是方法并以错误的方式进行线程化.
This is my code today.. and I'm worried that I'm using the methods and threading the wrong way.
- (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];
}
这是这样做的好方法吗,除了performSelectorOnMainThread之外,您还能指出其他更好的方法吗?
Is this a good way of doing this, is there anything besides performSelectorOnMainThread this you can point me to, that works better?
如您所见,在此示例中,我将nil传递给NSString参数,因为我必须传递一些东西,如果我在方法中没有NSString作为arg,则传递nil时应用程序将崩溃.这个?..请让我更清楚一点!
As you can see I pass nil to an NSString argument in this example, because I HAVE to pass something, if I don't have NSString as an arg in the method, the app crashes evan when passing nil.. Why is this?.. please make this a bit clearer for me!
//谢谢!
推荐答案
好,您已经在使用dispatch_async
.然后,您应该使用
Well, you're already using dispatch_async
. Then you should just use
dispatch_async(dispatch_get_main_queue(),^ { ... } );
从后台线程内部在主线程上执行操作.例如
from inside your background thread to perform things on the main thread. For example,
if ([self testServerMethod])
{
dispatch_async(dispatch_get_main_queue(),^ {
[self showMessageBoxWithString: @"Server is working!"];
[self threadedUIActivityRemover:nil];
} );
}else ...
它对您调用的方法的参数数量没有任何限制.
It doesn't have any constraint on the number of arguments for the methods you call.
这篇关于在目标C中执行selectoronmainthread的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!