184

I am testing some code that does asynchronous processing using Grand Central Dispatch. The testing code looks like this:

[object runSomeLongOperationAndDo:^{
    STAssert…
}];

The tests have to wait for the operation to finish. My current solution looks like this:

__block BOOL finished = NO;
[object runSomeLongOperationAndDo:^{
    STAssert…
    finished = YES;
}];
while (!finished);

Which looks a bit crude, do you know a better way? I could expose the queue and then block by calling dispatch_sync:

[object runSomeLongOperationAndDo:^{
    STAssert…
}];
dispatch_sync(object.queue, ^{});

…but that’s maybe exposing too much on the object.

skaffman
  • 381,978
  • 94
  • 789
  • 754
zoul
  • 96,282
  • 41
  • 242
  • 342

13 Answers13

312

Trying to use a dispatch_semaphore. It should look something like this:

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

[object runSomeLongOperationAndDo:^{
    STAssert…

    dispatch_semaphore_signal(sema);
}];

if (![NSThread isMainThread]) {
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
} else {
    while (dispatch_semaphore_wait(sema, DISPATCH_TIME_NOW)) { 
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0]]; 
    }
}

This should behave correctly even if runSomeLongOperationAndDo: decides that the operation isn't actually long enough to merit threading and runs synchronously instead.

Miguel Ruivo
  • 9,089
  • 3
  • 32
  • 62
kperryua
  • 10,334
  • 1
  • 36
  • 24
  • 61
    This code did not work for me. My STAssert would never execute. I had to replace the `dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);` with `while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW)) { [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:10]]; }` – nicktmro May 24 '12 at 08:19
  • 41
    That’s probably because your completion block is dispatched to the main queue? The queue is blocked waiting for the semaphore and therefore never executes the block. See [this question](http://stackoverflow.com/questions/10330679) about dispatching on the main queue without blocking. – zoul Jun 08 '12 at 09:38
  • 3
    I followed the suggestion of @Zoul &nicktmro. But it's looking that it's going to deadlock state. Test Case '-[BlockTestTest testAsync]' started. but never ended – NSCry Nov 09 '12 at 12:21
  • same for me following zoul's help – Jonesopolis Apr 15 '13 at 14:12
  • 3
    Do you need to release the semaphore under ARC? – Peter Warbo Apr 18 '13 at 22:49
  • 14
    this was exactly what I was looking for. Thanks! @PeterWarbo no you don't. The use of ARC removes the need to do a dispatch_release() – Hulvej Jul 10 '13 at 09:34
  • @zoul Does that mean the method nicktmro suggested is not reliable? – IluTov Sep 18 '13 at 17:15
  • It is, @NSAddict, provided that you don’t dispatch anything on the main queue before you signal the semaphore. – zoul Sep 19 '13 at 06:14
  • @zoul But that's the point. The framework I'm using is automatically dispatching back to the main queue as it's work is done. When I'm doing what nicktmro wrote it's all working fine, when I'm using the solution from the answer, the block which is dispatched on the main queue is never executed. What do you recommend? – IluTov Sep 19 '13 at 07:09
  • In that case I would use the busy wait solution, ie. spin the run loop as nicktmro suggests. You don’t have to bother with a semaphore, a simple boolean flag will do (as shown in the question). – zoul Sep 19 '13 at 07:53
  • 1
    Using `DISPATCH_TIME_FOREVER` is almost never the right solution. You're building in a deadlock should the code in your block ever encounter a situation where it doesn't reach the `dispatch_semaphone_signal` call. – Jeremy Wiebe Apr 15 '16 at 18:37
  • @nicktmro You should make your comment one of the answers! – dhruvm Jul 22 '16 at 19:05
  • ``` dispatch_release(semaphore);``` seems wrong. It notified me release is not available not available in automatic reference couting mode. Is there something wrong with your code? – Ryan Chou Aug 25 '16 at 09:44
  • Hi, Do you know if it's possible to use GCD to implement signal and wait between 2 different processes in macOS ? – Zohar81 Jul 19 '18 at 13:36
  • Optionally, you can call `dispatch_semaphore_create(1);` instead of `dispatch_semaphore_create(0)` and place the `dispatch_semaphore_wait` command above the block. This approach worked better for my case. It's similar to a do/while(condition). The answer above, on the other hand, is similar to while(condition)/do – marcelosalloum May 27 '19 at 18:24
  • ARC forbids explicit message send of 'release' – ikel Jul 17 '19 at 17:31
  • @nicktmro Do you recall why you use `dateWithTimeIntervalSinceNow:10` specifically? I've found it still seems to work with using `0` instead of `10` and was wondering if you had a strategic reason for using `10`? – Albert Renshaw Oct 15 '19 at 19:41
  • Based on the many comments here, and the age of this answer, I've updated it to use Kperryua's original solution when not on the main thread, but nicktmro's solution when on the main thread, as well as wrapping the memory management code in ARC detecting CLANG since that's mostly now obsolete. – Albert Renshaw Oct 15 '19 at 19:46
31

In addition to the semaphore technique covered exhaustively in other answers, we can now use XCTest in Xcode 6 to perform asynchronous tests via XCTestExpectation. This eliminates the need for semaphores when testing asynchronous code. For example:

- (void)testDataTask
{
    XCTestExpectation *expectation = [self expectationWithDescription:@"asynchronous request"];

    NSURL *url = [NSURL URLWithString:@"http://www.apple.com"];
    NSURLSessionTask *task = [self.session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        XCTAssertNil(error, @"dataTaskWithURL error %@", error);

        if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
            NSInteger statusCode = [(NSHTTPURLResponse *) response statusCode];
            XCTAssertEqual(statusCode, 200, @"status code was not 200; was %d", statusCode);
        }

        XCTAssert(data, @"data nil");

        // do additional tests on the contents of the `data` object here, if you want

        // when all done, Fulfill the expectation

        [expectation fulfill];
    }];
    [task resume];

    [self waitForExpectationsWithTimeout:10.0 handler:nil];
}

For the sake of future readers, while the dispatch semaphore technique is a wonderful technique when absolutely needed, I must confess that I see too many new developers, unfamiliar with good asynchronous programming patterns, gravitate too quickly to semaphores as a general mechanism for making asynchronous routines behave synchronously. Worse I've seen many of them use this semaphore technique from the main queue (and we should never block the main queue in production apps).

I know this isn't the case here (when this question was posted, there wasn't a nice tool like XCTestExpectation; also, in these testing suites, we must ensure the test does not finish until the asynchronous call is done). This is one of those rare situations where the semaphore technique for blocking the main thread might be necessary.

So with my apologies to the author of this original question, for whom the semaphore technique is sound, I write this warning to all of those new developers who see this semaphore technique and consider applying it in their code as a general approach for dealing with asynchronous methods: Be forewarned that nine times out of ten, the semaphore technique is not the best approach when encounting asynchronous operations. Instead, familiarize yourself with completion block/closure patterns, as well as delegate-protocol patterns and notifications. These are often much better ways of dealing with asynchronous tasks, rather than using semaphores to make them behave synchronously. Usually there are good reasons that asynchronous tasks were designed to behave asynchronously, so use the right asynchronous pattern rather than trying to make them behave synchronously.

Rob
  • 371,891
  • 67
  • 713
  • 902
  • 1
    I think this should be the accepted answer now. Here are the docs also: https://developer.apple.com/library/prerelease/ios/documentation/DeveloperTools/Conceptual/testing_with_xcode/testing_3_writing_test_classes/testing_3_writing_test_classes.html#//apple_ref/doc/uid/TP40014132-CH4-SW6 – hris.to Mar 13 '15 at 14:22
  • I've a question about this. I've got some asynchronous code that performs about a dozen AFNetworking download calls to download a single document. I'd like to schedule downloads on an `NSOperationQueue`. Unless I use something like a semaphore, the document download `NSOperation`s will all immediately appear to complete and there won't be any real queueing of downloads – they'll pretty much proceed concurrently, which I don't want. Are semaphores reasonable here? Or is there a better way to make NSOperations wait for the asynchronous end of others? Or something else? – Benjohn Jun 12 '15 at 12:09
  • No, don't use semaphores in this situation. If you have operation queue to which you are adding the `AFHTTPRequestOperation` objects, then you should then just create a completion operation (which you'll make dependent upon the other operations). Or use dispatch groups. BTW, you say you don't want them running concurrently, which is fine if that's what you need, but you pay serious performance penalty doing this sequentially rather than concurrently. I generally use `maxConcurrentOperationCount` of 4 or 5. – Rob Jun 12 '15 at 13:12
29

I’ve recently come to this issue again and wrote the following category on NSObject:

@implementation NSObject (Testing)

- (void) performSelector: (SEL) selector
    withBlockingCallback: (dispatch_block_t) block
{
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    [self performSelector:selector withObject:^{
        if (block) block();
        dispatch_semaphore_signal(semaphore);
    }];
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    dispatch_release(semaphore);
}

@end

This way I can easily turn asynchronous call with a callback into a synchronous one in tests:

[testedObject performSelector:@selector(longAsyncOpWithCallback:)
    withBlockingCallback:^{
    STAssert…
}];
zoul
  • 96,282
  • 41
  • 242
  • 342
25

Generally don't use any of these answers, they often won't scale (there's exceptions here and there, sure)

These approaches are incompatible with how GCD is intended to work and will end up either causing deadlocks and/or killing the battery by nonstop polling.

In other words, rearrange your code so that there is no synchronous waiting for a result, but instead deal with a result being notified of change of state (eg callbacks/delegate protocols, being available, going away, errors, etc.). (These can be refactored into blocks if you don't like callback hell.) Because this is how to expose real behavior to the rest of the app than hide it behind a false façade.

Instead, use NSNotificationCenter, define a custom delegate protocol with callbacks for your class. And if you don't like mucking with delegate callbacks all over, wrap them into a concrete proxy class that implements the custom protocol and saves the various block in properties. Probably also provide convenience constructors as well.

The initial work is slightly more but it will reduce the number of awful race-conditions and battery-murdering polling in the long-run.

(Don't ask for an example, because it's trivial and we had to invest the time to learn objective-c basics too.)

8

Here's a nifty trick that doesn't use a semaphore:

dispatch_queue_t serialQ = dispatch_queue_create("serialQ", DISPATCH_QUEUE_SERIAL);
dispatch_async(serialQ, ^
{
    [object doSomething];
});
dispatch_sync(serialQ, ^{ });

What you do is wait using dispatch_sync with an empty block to Synchronously wait on a serial dispatch queue until the A-Synchronous block has completed.

Leslie Godwin
  • 2,367
  • 22
  • 16
  • The problem with this answer is that it doesn't address the OP's original problem, which is that the API that needs to be used takes a completionHandler as an argument and returns immediately. Calling that API inside of this answer's async block would return immediately even though the completionHandler had not run yet. Then the sync block would be executing before the completionHandler. – BTRUE Apr 22 '17 at 13:06
6
- (void)performAndWait:(void (^)(dispatch_semaphore_t semaphore))perform;
{
  NSParameterAssert(perform);
  dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  perform(semaphore);
  dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
  dispatch_release(semaphore);
}

Example usage:

[self performAndWait:^(dispatch_semaphore_t semaphore) {
  [self someLongOperationWithSuccess:^{
    dispatch_semaphore_signal(semaphore);
  }];
}];
Oliver Atkinson
  • 7,647
  • 29
  • 41
2

There’s also SenTestingKitAsync that lets you write code like this:

- (void)testAdditionAsync {
    [Calculator add:2 to:2 block^(int result) {
        STAssertEquals(result, 4, nil);
        STSuccess();
    }];
    STFailAfter(2.0, @"Timeout");
}

(See objc.io article for details.) And since Xcode 6 there’s an AsynchronousTesting category on XCTest that lets you write code like this:

XCTestExpectation *somethingHappened = [self expectationWithDescription:@"something happened"];
[testedObject doSomethigAsyncWithCompletion:^(BOOL succeeded, NSError *error) {
    [somethingHappened fulfill];
}];
[self waitForExpectationsWithTimeout:1 handler:NULL];
zoul
  • 96,282
  • 41
  • 242
  • 342
1

Here is an alternative from one of my tests:

__block BOOL success;
NSCondition *completed = NSCondition.new;
[completed lock];

STAssertNoThrow([self.client asyncSomethingWithCompletionHandler:^(id value) {
    success = value != nil;
    [completed lock];
    [completed signal];
    [completed unlock];
}], nil);    
[completed waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
[completed unlock];
STAssertTrue(success, nil);
Peter DeWeese
  • 17,664
  • 8
  • 75
  • 98
  • 1
    There is an error in the above code. From the `NSCondition` [documentation](http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/NSCondition_class/Reference/Reference.html#//apple_ref/occ/instm/NSCondition/waitUntilDate:) for `-waitUntilDate:` "You must lock the receiver prior to calling this method." So the `-unlock` should be after `-waitUntilDate:`. – Patrick May 30 '13 at 00:39
  • This doesn't scale to anything that uses multiple threads or run queues. –  Sep 06 '14 at 01:54
0
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
[object blockToExecute:^{
    // ... your code to execute
    dispatch_semaphore_signal(sema);
}];

while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW)) {
    [[NSRunLoop currentRunLoop]
        runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0]];
}

This did it for me.

  • 3
    well, it causes high cpu usage though – kevin Mar 18 '14 at 03:38
  • 4
    @kevin Yup, this is ghetto polling that will murder the battery. –  Sep 06 '14 at 01:46
  • @Barry, how does it consume more battery. please guide. – pkc456 Sep 06 '17 at 18:43
  • @pkc456 Have a look in a computer science book about the differences between how polling and asynchronous notification work. Good luck. –  May 19 '18 at 10:05
  • 3
    Four and a half years later and with the knowledge and experience I've gained I would not recommend my answer. –  Jul 18 '18 at 12:28
0

Sometimes, Timeout loops are also helpful. May you wait until you get some (may be BOOL) signal from async callback method, but what if no response ever, and you want to break out of that loop? Here below is solution, mostly answered above, but with an addition of Timeout.

#define CONNECTION_TIMEOUT_SECONDS      10.0
#define CONNECTION_CHECK_INTERVAL       1

NSTimer * timer;
BOOL timeout;

CCSensorRead * sensorRead ;

- (void)testSensorReadConnection
{
    [self startTimeoutTimer];

    dispatch_semaphore_t sema = dispatch_semaphore_create(0);

    while (dispatch_semaphore_wait(sema, DISPATCH_TIME_NOW)) {

        /* Either you get some signal from async callback or timeout, whichever occurs first will break the loop */
        if (sensorRead.isConnected || timeout)
            dispatch_semaphore_signal(sema);

        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
                                 beforeDate:[NSDate dateWithTimeIntervalSinceNow:CONNECTION_CHECK_INTERVAL]];

    };

    [self stopTimeoutTimer];

    if (timeout)
        NSLog(@"No Sensor device found in %f seconds", CONNECTION_TIMEOUT_SECONDS);

}

-(void) startTimeoutTimer {

    timeout = NO;

    [timer invalidate];
    timer = [NSTimer timerWithTimeInterval:CONNECTION_TIMEOUT_SECONDS target:self selector:@selector(connectionTimeout) userInfo:nil repeats:NO];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}

-(void) stopTimeoutTimer {
    [timer invalidate];
    timer = nil;
}

-(void) connectionTimeout {
    timeout = YES;

    [self stopTimeoutTimer];
}
Khulja Sim Sim
  • 3,258
  • 1
  • 26
  • 27
  • 1
    Same problem: battery life fail. –  Sep 06 '14 at 01:54
  • 1
    @Barry Not sure even if you looked at the code. There is TIMEOUT_SECONDS period within which if async call doesn't respond, it will break the loop. That's the hack to break the deadlock. This code perfectly works without killing the battery. – Khulja Sim Sim Sep 09 '14 at 23:54
0

Very primitive solution to the problem:

void (^nextOperationAfterLongOperationBlock)(void) = ^{

};

[object runSomeLongOperationAndDo:^{
    STAssert…
    nextOperationAfterLongOperationBlock();
}];
CAHbl463
  • 82
  • 6
0

Swift 4:

Use synchronousRemoteObjectProxyWithErrorHandler instead of remoteObjectProxy when creating the remote object. No more need for a semaphore.

Below example will return the version received from the proxy. Without the synchronousRemoteObjectProxyWithErrorHandler it will crash (trying to access non accessible memory):

func getVersion(xpc: NSXPCConnection) -> String
{
    var version = ""
    if let helper = xpc.synchronousRemoteObjectProxyWithErrorHandler({ error in NSLog(error.localizedDescription) }) as? HelperProtocol
    {
        helper.getVersion(reply: {
            installedVersion in
            print("Helper: Installed Version => \(installedVersion)")
            version = installedVersion
        })
    }
    return version
}
Freek Sanders
  • 997
  • 8
  • 21
-1

I have to wait until a UIWebView is loaded before running my method, I was able to get this working by performing UIWebView ready checks on main thread using GCD in combination with semaphore methods mentioned in this thread. Final code looks like this:

-(void)myMethod {

    if (![self isWebViewLoaded]) {

            dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

            __block BOOL isWebViewLoaded = NO;

            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

                while (!isWebViewLoaded) {

                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((0.0) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        isWebViewLoaded = [self isWebViewLoaded];
                    });

                    [NSThread sleepForTimeInterval:0.1];//check again if it's loaded every 0.1s

                }

                dispatch_sync(dispatch_get_main_queue(), ^{
                    dispatch_semaphore_signal(semaphore);
                });

            });

            while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW)) {
                [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0]];
            }

        }

    }

    //Run rest of method here after web view is loaded

}
Albert Renshaw
  • 15,644
  • 17
  • 92
  • 173