这就是我的代码现在的样子,我想以串行方式调用这些方法:
-(void) methodOnBackThread // this method will run on a background thread
{
[runner runThisMethod]; // and this will run on the same background thread as well
[runner runThisOtherMethod]; // and so will this one
// but I want this one to run on the main thread :
[runner runThisMethodOnTheMainThreadUsing:thisParameter using:thisOtherParamater andUsing:thisOtherOneAsWell];
[runner runThisOtherMethod]; // this one will run on the background thread as well
// but I want this one to run on the main thread :
[runner runThisMethodOnTheMainThreadUsing:thisParameter using:thisOtherParamater andUsing:thisOtherOneAsWell];
[runner runThisOtherMethod]; // this one will run on the background thread as well
// etc..
}
我相信我必须使用 dispatch_get_main_queue
但我不知道如何在上述情况下实现它。
如何将[runner runThisMethodOnTheMainThreadUsing:thisParameter using:thisOtherParamater andUsing:thisOtherOneAsWell];
提交到主线程,然后返回执行我其余的后台方法,然后获取主线程下一个方法是否需要它?
最佳答案
如果您的目标是 iOS 4 及更高版本,请使用 grand central dispatch。你可以这样做:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//do some stuff here in the background
dispatch_async(dispatch_get_main_queue(), ^{
//do some stuff here in the main thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//do some stuff here in the background after finishing calling a method on the main thread
});
});
});
关于objective-c - 从在后台线程上执行的其他方法内部在主线程上执行方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12098285/