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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:06:04+00:00 2026-05-15T03:06:04+00:00

I kick off a network request, assuming no login credentials are required to talk

  • 0

I kick off a network request, assuming no login credentials are required to talk to the destination server. If they are required, then I get an authentication challenge, at which point I display a view requesting said credentials from the user. When they are supplied, I restart the network request, using those credentials.

That’s all fine and dandy, as long as I only do one request at a time. But I’m not, typically.

When both requests are kicked off, I get the first challenge, and present the prompt (using -presentModalViewController:). Then the 2nd challenge comes in. And I crash when it tries to display the 2nd prompt.

I have the bulk of this wrapped in an @synchronized() block, but this has no effect because these delegate methods are all being called on the same (main) thread. The docs say the delegate methods are called on the same thread in which the connection was started. OK, no problem; I’ll just write a method that I run on a background thread using -performSelectorInBackground:

NSURLConnection *connection = [[NSURLConnection alloc] 
                               initWithRequest:request 
                                      delegate:self
                              startImmediately:NO];
[connections addObject:connection];
[self performSelectorInBackground:@selector(startConnection:) 
                       withObject:connection];
[connection release];

- (void)startConnection:(NSURLConnection *)connection {
   NSAutoreleasePool *pool = [NSAutoreleasePool new];
   [connection scheduleInRunLoop:[NSRunLoop currentRunLoop] 
                         forMode:NSDefaultRunLoopMode];
   [connection start];
   [pool drain];
}

which should put every network request, and its callbacks, on its own thread, and then my @synchronized() blocks will take effect.

The docs for -initWithRequest:… state “Messages to the delegate will be sent on the thread that calls this method. By default, for the connection to work correctly the calling thread’s run loop must be operating in the default run loop mode.” Ok, I’m doing that.

They also state “If you pass NO [for startImmediately], you must schedule the connection in a run loop before starting it.” OK, I’m doing that, too.

Furthermore, the docs for NSRunLoop state “Each NSThread object, including the application’s main thread, has an NSRunLoop object automatically created for it as needed. If you need to access the current thread’s run loop, you do so with the class method currentRunLoop.” I’m assuming this applies to the background thread created by the call -performSelectorInBackground… (which does appear to be the case, when I execute ‘po [NSClassFromString(@”NSRunLoop”) currentRunLoop]’ in the -startConnection: method).

The -startConnection: method is indeed being called. But after kicking off the connection, I now never get any callbacks on it. None of the -connectionDid… delegate methods. (I even tried explicitly starting the thread’s run loop, but that made no difference; I’ve used threads like this before, and I’ve never had to start the run loop manually before–but I’m now grasping at straws…)

I think I’ve come up with a workaround such that I only handle one request at a time, but it’s kludgy and I’d like to do this the Right Way. But, what am I missing here?

Thanks!
randy

  • 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-15T03:06:05+00:00Added an answer on May 15, 2026 at 3:06 am

    Is the [pool drain]; line reached? I’m guessing it is. After this the -startConnection method exits, at that point the background thread is finished and destroyed. This is probably not what you want…

    You have to run the runloop. You say you did this and it made no difference. This means you did it wrong. Read the Runloop section in the Threaded Programming Guide (It is better to go to the proper docs than hear it off me). Beware, useful methods like -performSelectorInBackground might make multithreading seem appealing, but it is difficult.

    Also, what do you mean

    ‘wrapped in an @synchronized() block,
    but this has no effect because these
    delegate methods are all being called
    on the same (main) thread’

    ?

    @synchronized() will do the same on the main thread as it does on a background thread.. what do you think it does?

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

Sidebar

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.