dispatch_sync on main queue hangs in unit test

前端 未结 5 1085
太阳男子
太阳男子 2020-12-02 11:08

I was having some trouble unit testing some grand central dispatch code with the built in Xcode unit testing framework, SenTestingKit. I managed to boil my problem done to t

相关标签:
5条回答
  • 2020-12-02 11:38

    The problem in your code is that no matter whether you use dispatch_sync or dispatch_async , STFail() will always be called, causing your test to fail.

    More importantly, as BJ Homer's explained, if you need to run something synchronously in the main queue, you must make sure you are not in the main queue or a dead-lock will happen. If you are in the main queue you can simply run the block as a regular function.

    Hope this helps:

    - (void)testSample {
    
        __block BOOL didRunBlock = NO;
        void (^yourBlock)(void) = ^(void) {
            NSLog(@"on main queue!");
            // Probably you want to do more checks here...
            didRunBlock = YES;
        };
    
        // 2012/12/05 Note: dispatch_get_current_queue() function has been
        // deprecated starting in iOS6 and OSX10.8. Docs clearly state they
        // should be used only for debugging/testing. Luckily this is our case :)
        dispatch_queue_t currentQueue = dispatch_get_current_queue();
        dispatch_queue_t mainQueue = dispatch_get_main_queue();
    
        if (currentQueue == mainQueue) {
            blockInTheMainThread();
        } else {
            dispatch_sync(mainQueue, yourBlock); 
        }
    
        STAssertEquals(YES, didRunBlock, @"FAIL!");
    }
    
    0 讨论(0)
  • 2020-12-02 11:39

    If you are on the main queue and synchronously wait for the main queue to be available you will indeed wait a long time. You should test to make sure you are not already on the main thread.

    0 讨论(0)
  • 2020-12-02 11:41

    dispatch_sync runs a block on a given queue and waits for it to complete. In this case, the queue is the main dispatch queue. The main queue runs all its operations on the main thread, in FIFO (first-in-first-out) order. That means that whenever you call dispatch_sync, your new block will be put at the end of the line, and won't run until everything else before it in the queue is done.

    The problem here is that the block you just enqueued is at the end of the line waiting to run on the main thread—while the testSample method is currently running on the main thread. The block at the end of the queue can't get access to the main thread until the current method (itself) finishes using the main thread. However dispatch_sync means Submits a block object for execution on a dispatch queue and waits until that block completes.

    0 讨论(0)
  • 2020-12-02 11:44

    To follow up, since

    dispatch_get_current_queue()
    

    is now deprecated, you can use

    [NSThread isMainThread]
    

    to see if you are on the main thread.

    So, using the other answer above, you could do:

    - (void)testSample 
    {
        BOOL __block didRunBlock = NO;
        void (^yourBlock)(void) = ^(void) {
            NSLog(@"on main queue!");
            didRunBlock = YES;
        };
    
        if ([NSThread isMainThread])
            yourBlock();
        else
            dispatch_sync(dispatch_get_main_queue(), yourBlock);
    
        STAssertEquals(YES, didRunBlock, @"FAIL!");
    }
    
    0 讨论(0)
  • 2020-12-02 11:53

    Will you ever get out of house if you must wait for yourself to get out house first? You guessed right! No! :]

    Basically if:

    1. You are on FooQueue. (doesn't have to be main_queue)
    2. You call the method using sync ie in a serial way and want to execute on FooQueue.

    It will never happen for same reason that you will never get out of house!

    It won't ever get dispatched because it has to wait for itself to get off the queue!

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