Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8086253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:23:29+00:00 2026-06-05T18:23:29+00:00

I ran into a scenario where I had a delegate callback which could occur

  • 0

I ran into a scenario where I had a delegate callback which could occur on either the main thread or another thread, and I wouldn’t know which until runtime (using StoreKit.framework).

I also had UI code that I needed to update in that callback which needed to happen before the function executed, so my initial thought was to have a function like this:

-(void) someDelegateCallback:(id) sender
{
    dispatch_sync(dispatch_get_main_queue(), ^{
        // ui update code here
    });

    // code here that depends upon the UI getting updated
}

That works great, when it is executed on the background thread. However, when executed on the main thread, the program comes to a deadlock.

That alone seems interesting to me, if I read the docs for dispatch_sync right, then I would expect it to just execute the block outright, not worrying about scheduling it into the runloop, as said here:

As an optimization, this function invokes the block on the current thread when possible.

But, that’s not too big of a deal, it simply means a bit more typing, which lead me to this approach:

-(void) someDelegateCallBack:(id) sender
{
    dispatch_block_t onMain = ^{
        // update UI code here
    };

    if (dispatch_get_current_queue() == dispatch_get_main_queue())
       onMain();
    else
       dispatch_sync(dispatch_get_main_queue(), onMain);
}

However, this seems a bit backwards. Was this a bug in the making of GCD, or is there something that I am missing in the docs?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:23:31+00:00Added an answer on June 5, 2026 at 6:23 pm

    I found this in the documentation (last chapter):

    Do not call the dispatch_sync function from a task that is executing
    on the same queue that you pass to your function call. Doing so will
    deadlock the queue. If you need to dispatch to the current queue, do
    so asynchronously using the dispatch_async function.

    Also, I followed the link that you provided and in the description of dispatch_sync I read this:

    Calling this function and targeting the current queue results in deadlock.

    So I don’t think it’s a problem with GCD, I think the only sensible approach is the one you invented after discovering the problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ran into something interesting, want to know if I'm doing something wrong or if
I ran into an issue. Below is my code, which asks user for input
Ran into another challenge. I looked through some of the questions that I found
Hoping someone has ran into this scenario and can lead me in the right
I ran into a scenario where LINQ to SQL acts very strangely. I would
I've ran into this scenario a few times now and I cant explain it.
I ran into this very odd scenario. This won't hide the H1: if ($('#content
Today I ran into a scenario where I have to create a method that
I have ran into quite an interesting mapping scenario. I have two entities (
(Rhetorical Question) I ran into a bizarre scenario today where I wanted PHP to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.