在继续执行之前如何确保异步操作完成 [英] How to ensure completion of a async operation before continuing with execution

查看:215
本文介绍了在继续执行之前如何确保异步操作完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个iOS应用,其部分代码依赖于特定任务返回的成功/失败值,该任务涉及库中的回调.我希望仅在回调返回成功/失败之后才返回此任务的返回值.但是,由于我编写了顺序代码,因此即使在回调返回成功/失败之前,也会返回返回值.

I am building an iOS app and some part of its code relies on the success/failure value returned from a particular task.This tasks involves callbacks from a library. I want the return value from this task to be returned only after the callback has returned either success/failure. But since I wrote a sequential code the return value is returned even before the callback returns a success/failure.

我研究了使用模态视图控制器,据我了解,我可以使任务从该视图控制器执行,然后将代码返回.

I looked into using modal view controllers and from what I understand I can make the task execute from this view controller and then return the code back.

但是这也不符合我的要求,因为当执行启动回调序列的代码时,我不希望显示新的视图控制器.尽管有一定的回调要求我提示用户输入信息.我在弹出窗口中执行此操作,并且考虑在弹出窗口模式内制作视图控制器.但是然后,回调仍将是主线程的一部分,并且当我的弹出窗口以模态形式呈现时,我将不会收到回调.(p).

But this also doesn't suit my requirements as when the code which initiates the callback sequence is executed I don't want a new view controller to be displayed. Although there is a certain callback which requires me to prompt the user for information. I do this in a popover and I considered making the view controller within the popover modal. But then the callbacks will still be part of the main thread and I won't receive them when my popover is presented modally(?).

根据我目前对这些概念的理解,我不知道如何进行.在iOS中有什么方法可以做到这一点?

With my current understanding of these concepts I don't know how to proceed. Is there some way to do this in iOS?

该代码做了这样的事情

The code does something like this

//In CustomTableViewController

-(void) someFunc
{
   ENUM_NAME code = [TaskController startTheTask:args];
   if(code == SUCCEEDED)
   {
      //Do Something
   }
   if(code == FAILED)
   {
      //Do Something Else
   }
}

//In TaskController

-(ENUM_NAME) startTheTask:args
{
   startWorkflow(args); //This function registers callback function with the library. 
   return finalCode; //This is returned even before it is set to SUCCEEDED/FAILED
}

-(void) onCallback:params
{
    MSG_TYPE msg = [params getMsg];

    if(msg == TASK_FAILED)
       finalCode = FAILED;
    if(msg == TASK_SUCCEEDED)
       finalCode = SUCCEEDED;
    if(msg == TASK_SHOW_PROMPT)
    {  
       [PopOverController showPopOver];
    }
}

-(void) onUserInfoAdded
{
    //This is called when Confirm is clicked in the popover
    continueWorkflow(params); //asks for the next callback to happen
}

-(void) onCancleClicked
{
    //This is called when Popover is dismissed without entering Info
    cancleWorkflow(params); //asks for result of the workflow through callback
}

推荐答案

您可以使用GCD.例如:

You can use GCD. For example:

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();

    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{           
         //put one process here
         dispatch_group_leave(group);  //when done
    });

    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{           
         //put another process here
         dispatch_group_leave(group); //when done
    });

    // All updates finished
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
         // add last steps here after all processess are finished
    });

    dispatch_release(group);

这篇关于在继续执行之前如何确保异步操作完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆