using dispatch_sync in Grand Central Dispatch

前端 未结 8 2095
孤城傲影
孤城傲影 2020-11-30 17:25

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 u

8条回答
  •  清歌不尽
    2020-11-30 17:34

    dispatch_sync is mainly used inside dispatch_async block to perform some operations on main thread(like update ui).

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //Update UI in main thread
        dispatch_sync(dispatch_get_main_queue(), ^{
          self.view.backgroundColor = color;
        });
    });
    

提交回复
热议问题