objective-c - 从在后台线程上执行的其他方法内部在主线程上执行方法?

标签 objective-c ios multithreading nsoperationqueue

这就是我的代码现在的样子,我想以串行方式调用这些方法:

-(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/

相关文章:

iphone - 在 GDB 中访问静态方法

ios - UIPageViewController 滑动延迟

multithreading - 异步方法和线程之间有什么区别?

c - 哪个是浏览器选项卡 : multi-thread or multi-process? 的更好选择

java - ThreadPoolExecutor.execute(Runnable command) 何时创建新线程

objective-c - 导入并包含在头文件中 - 什么时候可以?

iphone - 仅对 subview 启用 UserInteraction

iphone - 当应用进入前台、解锁或通过通知访问时调用方法

android - 设备间近场通信

ios - 升级到 Swift 3 : cannot override 'init' which has been marked unavailable