Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在调用异步API时等待某些操作完成_Ios_Objective C_Macos_Cocoa - Fatal编程技术网

Ios 如何在调用异步API时等待某些操作完成

Ios 如何在调用异步API时等待某些操作完成,ios,objective-c,macos,cocoa,Ios,Objective C,Macos,Cocoa,我们有一些操作集,但每个操作都调用异步API。我们希望等到异步API返回,然后开始执行第二个操作 例:我们有X、Y和Z动作:方法1做X动作,方法2做Y动作,方法3做Z动作。这里Method1在内部调用一些异步API。所以我们不希望在Method1完成之前调用Method2 method1 () // Here wait till method1 complete method2 () // Here wait till method12 complete method3 ()

我们有一些操作集,但每个操作都调用异步API。我们希望等到异步API返回,然后开始执行第二个操作

例:我们有X、Y和Z动作:方法1做X动作,方法2做Y动作,方法3做Z动作。这里Method1在内部调用一些异步API。所以我们不希望在Method1完成之前调用Method2

method1 ()

// Here wait till method1 complete 

method2 ()

// Here wait till method12 complete 

method3 () 

什么可以用来等待方法1完成。Objective-C的哪种机制更有效?
提前感谢

只需在主线程中调用您的方法,因为异步API在后台线程中处理。

您可以使用一个
调度信号量,它在块(异步完成块)的末尾发出信号。此外,如果method1、method2、method3总是按顺序被调用,那么它们需要共享信号量,我会将整个过程移动到单个方法。下面是一个如何做到这一点的示例:

- (void) method123
{
    dispatch_semaphore_t waitSema = dispatch_semaphore_create(0);

    callAsynchAPIPart1WithCompletionBlock(^(void) {
        // Do your completion then signal the semaphore
        dispatch_semaphore_signal(waitSema);
    });

    // Now we wait until semaphore is signaled.
    dispatch_semaphore_wait(waitSema, DISPATCH_TIME_FOREVER); 

    callAsynchAPIPart2WithCompletionBlock(^(void) {
        // Do your completion then signal the semaphore
        dispatch_semaphore_signal(waitSema);
    });

    // Now we wait until semaphore is signaled.
    dispatch_semaphore_wait(waitSema, DISPATCH_TIME_FOREVER); 

    callAsynchAPIPart3WithCompletionBlock(^(void) {
        // Do your completion then signal the semaphore
        dispatch_semaphore_signal(waitSema);
    });

    // Now we wait until semaphore is signaled.
    dispatch_semaphore_wait(waitSema, DISPATCH_TIME_FOREVER); 

    dispatch_release(waitSema);
}
或者,您可以通过完成块将调用链接起来,如下所示:

- (void) method123
{
    callAsynchAPIPart1WithCompletionBlock(^(void) {
        // Do completion of method1 then call method2
        callAsynchAPIPart2WithCompletionBlock(^(void) {
            // Do completion of method2 then call method3
            callAsynchAPIPart1WithCompletionBlock(^(void) {
                // Do your completion
            });
        });
    });
}

为此,您可以使用操作队列,谢谢回复,还有其他建议……而且您也没有任何didfinish类型的委托?不,我们没有任何didfinish类型的委托,IshankAre method1、2、3私有API,或者您可以更改它们?谢谢,我将尝试一下
- (void) method123
{
    callAsynchAPIPart1WithCompletionBlock(^(void) {
        // Do completion of method1 then call method2
        callAsynchAPIPart2WithCompletionBlock(^(void) {
            // Do completion of method2 then call method3
            callAsynchAPIPart1WithCompletionBlock(^(void) {
                // Do your completion
            });
        });
    });
}