Regarding why your user interface is blocked, this is less clear. In any case, you do not even need to send doSync to any global queue, because Alamofire is asynchronous. It makes no sense to send a call that is already asynchronous to the background queue.
The only thing that looks suspicious is a recursive call from repeatBlock back to repeatBlock . If an Alamofire call does not start asynchronously (i.e., it returns caching results or some error), theoretically you could spin in the queue that Alamofire uses. I would suggest sending a recursive call to repeatBlock from repeatBlock using async to avoid possible problems.
Another thing you can do is provide the queue parameter for the Alamofire request , ensuring that it will not use the main queue. Going to his own devices, he calls completion handlers in the main queue (which is usually very useful, but can cause problems in your script). I suggest trying to supply a non-primary queue as the queue request parameter.
If it still blocks, I suggest either running it on the system track of the tools and see which blocking calls are in the main queue. Or you can just start the application, pause it while the user interface is locked, and then look at the stack trace for the main thread, and you can see where it blocks.
Finally, we should consider the possibility that the main queue lock will not be saved in the above code. Assuming that you are not suffering from a degenerate situation where Alamofire immediately calls the completion handler, the above code is unlikely to block the main queue. The above diagnostics should confirm this, but I could suggest you expand your search to identify other things that would block the main queue:
- Any
sync calls from a serial queue (for example, the main queue) to a serial queue are a likely problem. - Candidates for any locks, semaphores, or send groups are also likely.
- You have ways in which you don't call
repeatBlock recursively and don't call finishSync ... you are sure that the main queue is locked, and this is not just a question that it never calls finishSync in some execution paths. "
Bottom line, make sure that the problem actually lies in blocking the main thread in the above code. I suspect this is not the case.