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 8840287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:25:55+00:00 2026-06-14T10:25:55+00:00

I am using GCD to send HTTP request asynchronously. Here is the code that

  • 0

I am using GCD to send HTTP request asynchronously. Here is the code that doesn’t work:

dispatch_async(connectionQueue, ^{
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

        [request setURL:[NSURL URLWithString:[NSString stringWithFormat:someURL]]];


        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        [connection start];//Not working
    }); 

the above code is not working at all. I am not getting any call back in NSURLConnectionDelegate’s methods.

But when i tried the following code, everything worked fine and i got proper callbacks

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

[request setURL:[NSURL URLWithString:[NSString stringWithFormat:someURL]]];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

dispatch_async(connectionQueue, ^{

    [connection start]; // working fine. But WHY ????
});

Can some one please explain this weird behavior of block/GCD?

  • 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-14T10:25:56+00:00Added an answer on June 14, 2026 at 10:25 am

    Try this in the first part of your code sample-

    dispatch_async(dispatch_get_main_queue(), ^(void){
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        [connection start];
    }
    

    If you put the connection in a background queue, it gets shoved away after the queue is complete, and thus you don’t get your delegate callbacks. The connection can be in the main queue so it stays in the main run loop for callbacks to occur. Or, you can create your own runloop that handles your background operation for you as suggested by others.

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

Sidebar

Related Questions

I have a class that is a singleton. This singleton is instantiated using GCD
I have an object, that on initialization creates a serial GCD queue using dispatch_queue_create
I am currently using GCD. However, I've heard that NSOperation is actually a higher
I am using GCD to start a long-running background process ('run_loop') that creates an
How can I run code block in background periodically using GCD? I am trying
I'm using GCD's dispatch_after method when my application is being loaded to perform some
i am stuck with my first GCD and first core-data using application =) two
I want to use gcd function of the Integer class. Using the example from
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
I am using a pretty decent tutotial I have found for GCD, in it

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.