问题
Which thread does the block in dispatch_once run in? Can dispatch_once block run in background thread if the code is run from main thread? How do I ensure it runs on the main thread regardless of which thread executes it?
回答1:
It runs in the current/calling thread. If you wanted I suppose you could use dispatch_sync
to ensure it runs on a background thread but I'm not sure what that'd get you. In sum, it runs in the current thread. If another thread is already in the dispatch_once
block, the calling thread will block until the block finishes executing on the other thread, and then proceed.
来源:https://stackoverflow.com/questions/25897108/which-thread-does-the-block-in-dispatch-once-run-in