Objective C wait until an asynch task has been processed

后端 未结 2 2023
甜味超标
甜味超标 2021-01-25 21:36

In my code, I am running a local server (CocoaHTTPServer). When the server receives a request, it creates a thread and passes control to a certain method ( - (NSObject<

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-25 21:58

    You can use semaphore to block execution of the current queue until another one returns.
    The basic pattern is:

    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    [assetsLibrary enumerateAssetsUsingBlock^(ALAsset *result, NSUInteger index, BOOL *stop):^{
        ...
        dispatch_semaphore_signal(semaphore);
    }];
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    dispatch_release(semaphore);
    

    You can find a full example of this method in Apple's MTAudioProcessingTap Xcode Project: https://developer.apple.com/library/ios/samplecode/AudioTapProcessor
    The relevant lines start at MYViewController.m:86

提交回复
热议问题