Objective C wait until an asynch task has been processed

后端 未结 2 2024
甜味超标
甜味超标 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:55

    NSRunLoop has a method called runUntilDate: which should work for you with dates in near future like 1s ahead or so. That way you can replace your sleep call in the while loop for example with:

    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeintervalSinveNow:1]
    
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题