Can anyone explain with really clear use cases what the purpose of dispatch_sync in GCD is for? I can’t understand where and why I would have to use this.
Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You use it when you want to execute a block and wait for the results.
One example of this is the pattern where you’re using a dispatch queue instead of locks for synchronization. For example, assume you have a shared NSMutableArray
a, with access mediated by dispatch queueq. A background thread might be appending to the array (async), while your foreground thread is pulling the first item off (synchronously):