dispatch_async block on main queue is never execeuted

前端 未结 3 1324
一整个雨季
一整个雨季 2021-02-12 23:12

I have an app that uses a connection queue that handles the connections on a background thread. Each connection sends a JSON post, then when it receives a success, saves some o

相关标签:
3条回答
  • 2021-02-12 23:58

    If the block on the main thread is not executed, then it is because of 1 of 2 reasons.

    1. The main thread is blocked; is not processing any events at all. Got a while() loop on the main thread? That'd do it. A lock? There you go.

    2. The main thread is running a modal run loop inside the outer run loop. Asynchronous dispatches to the main event loop -- main thread -- won't be processed in this case.

    Set a breakpoint on that dispatch_async() and see what the main thread is doing (at the point of dispatch the main thread is most likely already in the bad state).

    DarkDust's suggestion of using dispatch_after() is a good one, but is unlikely to work in that it is almost assuredly the case that your main thread is not processing events when the problem occurs. I.e. fix the problem, then move to dispatch_after() as DarkDust suggests.

    0 讨论(0)
  • 2021-02-13 00:12

    I believe this is a great discussion. I came across this when I had the following code:

       dispatch_synch(dispatch_get_main_queue()){
            print("I am here")
       }
    

    the print code did not execute as I was dispatching a 'synch' block on the serial main thread which caused a dead lock. print was waiting for the dispatch to finish and dispatch was waiting for print to finish. When you dispatch in the main serial queue then you should use dispatch_async. and i guess if you use a concurrent queue then dispatch synch suits better

    0 讨论(0)
  • 2021-02-13 00:14

    If your main thread is busy with modal runloop, then you could try

    CFRunLoopPerformBlock(CFRunLoopGetMain(), kCFRunLoopCommonModes, block
        });
    
    0 讨论(0)
提交回复
热议问题