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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:28:05+00:00 2026-05-28T05:28:05+00:00

I know that if I create an NSURLConnection (standard async one), it will call

  • 0

I know that if I create an NSURLConnection (standard async one), it will call back on the same thread. Currently this is on my main thread. (work fine too).

But i’m now using the same code for something else, and I need to keep my UI snappy….

If i do

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    /* and inside here, at some NSURLConnection is created */


});

.. is it possible that my NSURLConnection is created but my thread disappears before the url connection has returned?

I’m new to GCD. How would one keep the thread alive until my url connection returned, or is there a better way I could be doing this?

  • 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-05-28T05:28:05+00:00Added an answer on May 28, 2026 at 5:28 am

    So really the issue isn’t the lifetime of the thread on which your block runs, it’s the fact that this particular thread is not going to have a runloop configured and running to receive any of the events coming back from the connection.

    So how do you solve this? There are different options to think about. I can list a few, and I’m sure others will list more.

    1 – You could use a synchronous connection here. One disadvantage is that you won’t get callbacks for authentication, redirection, caching, etc. (All the normal disadvantages of synchronous connections.) Plus each connection will of course block a thread for some period of time, so if you’re doing a lot of these then you could potentially have a few threads blocked at once, which is expensive.

    2 – If your connection is simple and you are using iOS5 then you can use this method:

    + (void)sendAsynchronousRequest:(NSURLRequest *)request
                              queue:(NSOperationQueue*) queue
                  completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))
    

    This will start an asynchronous connection and then allow you to specify a completion handler (for success or failure) and a NSOperationQueue on which you want that block to be scheduled.

    Again, you have the disadvantages of not getting the callbacks you might need for authentication, caching, etc. But at least you don’t have threads hanging around blocked by connections that are in flight.

    3 – Another option for iOS5 is to set the queue for all delegate callbacks:

    - (void)setDelegateQueue:(NSOperationQueue*) queue NS_AVAILABLE(10_7, 5_0);

    If you use this, then all of the delegate methods will be executed in the context of whatever NSOperationQueue you specify. So this is similar to option #2, expect that you get all of the delegate methods now to handle authentication, redirection, etc.

    4 – You could set up your own thread that you control specifically for managing these connections. And in setting up that thread, you configure a runloop appropriately. This would work fine in iOS4 and 5 and obviously gives you all of the delegate callbacks that you want to handle

    5 – You might think about what parts of your asynchronous connection handling are really interfering with your UI. Typically kicking off the connection or receiving delegate callbacks are not that expensive. The expensive (or indeterminate) cost is often in the processing of the data that you collect at the end. The question to ask here is are you really saving time by scheduling a block on some queue just to start an asynchronous connection that will go off immediately and do its thing on another thread anyway?

    So you could just start the connection from the main thread, and receive all of the delegate callbacks on the main thread, and then in your implementation of those delegate methods fire off whatever expensive work you need to do on some other queue or thread.

    So something like this:

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {      
            // go ahead and receive this message on the main thread
            // but then turn around and fire off a block to do the real expensive work
    
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    
             // Parse the data we've been collecting
    
            });
    
        }
    

    Again, this is not comprehensive. There are many ways to handle this, depending on your specific needs here. But I hope these thoughts help.

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

Sidebar

Related Questions

I know that one of the main principles of REST is that you create
I know that horizontal partitioning...you can create many tables. How can you do this
I know that in a standalone application I create one of the application context
i know that if you create a foreign key on a field (parent_id) in
I'm using python 3.1.1. I know that I can create byte objects using the
I know that we should only create repositories for Aggregate Roots and not for
I know that it is possible (in theory) to create a new type at
I know that it is possible to create .xls files from Delphi using ADO,
I know that using SharePoint internally is free, but what if I create an
I know that we can't overload operator with other meaning, we can't create new

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.