Objective-C顺序执行两个完成块

时间:2018-05-25 18:28:59

标签: objective-c grand-central-dispatch completion-block

我目前正致力于让DJI产品自主执行航点任务,并从DJI教程(https://developer.dji.com/mobile-sdk/documentation/ios-tutorials/GSDemo.html)进行调整。所以我试图将所有流程集成到一个功能中。以下是我必须整合的两个完成块:

pg_dump --cluster=9.6/main books > books.out

pg_lsclusters

为了使第二个成功运行,第一个必须首先完全执行。这似乎不是一个难题,但在尝试添加延迟或调度后我无法弄明白。

任何帮助都表示赞赏。谢谢。

2 个答案:

答案 0 :(得分:1)

来自the iOS version of the docs you linked-[DJIWaypointMissionOperator uploadMissionWithCompletion:]的文档说:

  

如果成功启动,请使用addListenerToUploadEvent:withQueue:andBlock接收详细进度。

所以,你会做这样的事情:

[[self missionOperator] uploadMissionWithCompletion:^(NSError * _Nullable error) {
    if (error)
    {
        ShowMessage(@"Upload Mission failed", error.description, @"", nil, @"OK");
    }
    else
    {
        ShowMessage(@"Upload Mission Started", @"", @"", nil, @"OK");

        [[self missionOperator] addListenerToUploadEvent:self
                                               withQueue:nil
                                                andBlock:^(DJIWaypointMissionUploadEvent *event){
            if (event.currentState == DJIWaypointMissionStateReadyToExecute)
            {
                [[self missionOperator] startMissionWithCompletion:^(NSError * _Nullable error) {
                    if (error)
                    {
                        ShowMessage(@"Start Mission Failed", error.description, @"", nil, @"OK");
                    }
                    else
                    {
                        ShowMessage(@"Mission Started", @"", @"", nil, @"OK");
                    }
                }];
            }
            else if (event.error)
            {
                ShowMessage(@"Upload Mission failed", event.error.description, @"", nil, @"OK");
            }
        }];
    }
}];

答案 1 :(得分:0)

您的代码看起来正确。我遇到了同样的问题,任务上传完毕后,我的missionOperator的currentState会还原为DJIWaypointMissionStateReadyToUpload而不是DJIWaypointMissionStateReadyToExecute。该任务已通过有效性检查,但实际上由于对各个航路点(cornerRadiusInMeters属性)的无效弯道要求而无效。

从文档中:

/**
 *  Corner radius of the waypoint. When the flight path mode  is
 *  `DJIWaypointMissionFlightPathCurved` the flight path near a waypoint will be  a
 *  curve (rounded corner) with radius [0.2,1000]. When there is a corner radius,
 *  the aircraft will never  go through the waypoint. By default, the radius is 0.2
 *  m. If the corner is made of three adjacent waypoints (Short for A,B,C)  . Then
 *  the radius of A(short for Ra) plus radius of B(short for Rb) must be smaller
 *  than the distance between  A and B. The radius of the first and the last
 *  waypoint in a mission does not affect the flight path and it should keep the
 *  default value (0.2m).
 */

希望这对某人有帮助。