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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:32:00+00:00 2026-05-11T20:32:00+00:00

I was just recently futzing about with a sample application trying to get my

  • 0

I was just recently futzing about with a sample application trying to get my head completely wrapped around NSRunLoop. The sample I wrote created a simple secondary thread via NSOperation. The secondary thread does a few tasks such as process an NSTimer and also some rudimentary streaming with NSStream. Both of these input sources require a properly configured NSRunLoop in order to execute.

My question is this. Originally I had some code that looked like this in the secondary thread:

NSRunLoop* myRunLoop = [NSRunLoop currentRunLoop];
self.connectTimer = [NSTimer scheduledTimerWithTimeInterval:connectTimeout
                                                     target:self
                                                   selector:@selector(connectionConnectedCheck:)
                                                   userInfo:nil 
                                                    repeats:NO];

[myRunLoop addTimer:self.connectTimer forMode:NSDefaultRunLoopMode]; // added source here
[myRunLoop run];

[NSStream getStreamsToHostNamed:relayHost port:relayPort inputStream:&inputStream outputStream:&outputStream];
if ((inputStream != nil) && (outputStream != nil))
{
    sendState = kSKPSMTPConnecting;
    isSecure = NO;

    [inputStream retain];
    [outputStream retain];

    [inputStream setDelegate:self];
    [outputStream setDelegate:self];

    [inputStream scheduleInRunLoop: myRunLoop //[NSRunLoop currentRunLoop]  
                                    forMode:NSRunLoopCommonModes];
    [outputStream scheduleInRunLoop: myRunLoop //[NSRunLoop currentRunLoop] 
                                    forMode:NSRunLoopCommonModes];

    [inputStream open];
    [outputStream open];

    self.inputString = [NSMutableString string];



    return YES;
}

Now, with the code above, the events would never process. Not on currentRunLoop. I did something terrible afterwards, as this was just an educational exercise, and modified it to run under NSRunLoop mainRunLoop. Worked like magic. However, I’m almost certain that depending on my main threads run loop in a secondary thread is on 10 different levels of wrong.

So my question is in two parts, and I hope that’s OK.

  1. What can possibly go wrong with the little ‘hack’ I applied in order to get the secondary thread to run and respond to events via the run loop?

  2. What’s the proper way to configure the secondary thread to listen to all events/timer based sources so I don’t have to do step 1.

Thanks for the insight all.

  • 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-11T20:32:00+00:00Added an answer on May 11, 2026 at 8:32 pm

    Answering your questions in reverse order:

    2. You’ve got two problems. The documentation for -[NSRunLoop run] says:

    If no input sources or timers are
    attached to the run loop, this method
    exits immediately; otherwise, it runs
    the receiver in the
    NSDefaultRunLoopMode by repeatedly
    invoking runMode:beforeDate:. In other
    words, this method effectively begins
    an infinite loop that processes data
    from the run loop’s input sources and
    timers.

    So using your thread’s own run loop, there are probably not input sources defined for the run loop, so it is returning immediately. If there are, your run loop is looping infinitely, and the rest of your code after that point is never being executed.

    In order to get things to work correctly, your run loop needs to first have some input sources, and then needs to run periodically to check for events. Note that you don’t want to use [NSRunLoop run], as you’ll never get control back. Instead, I would recommend setting up a loop right before return YES that continually runs the run loop until the thread has been cancelled, or until you’re done streaming data. That will allow the run loop to process data as it arrives. Something like this:

    while (!done) {
        [myRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
    }
    

    [NSRunLoop runUntilDate:] processes events until the specified date, and then returns control to your program so that you can do whatever you want.

    1. It’s working because your main thread’s run loop is being run periodically, so events are being handled. If your main thread ever blocked, however, your data would stop arriving. This could be particularly bad if the main thread was waiting for data from your second thread. Also, NSRunLoop is not thread-safe:

    Warning: The NSRunLoop class is
    generally not considered to be
    thread-safe and its methods should
    only be called within the context of
    the current thread. You should never
    try to call the methods of an
    NSRunLoop object running in a
    different thread, as doing so might
    cause unexpected results.
    (from the NSRunLoop documentation.)

    Apple’s Threading Programming Guide has a section entitled Run Loop Management, which explains all of this to some degree. It’s not the clearest document I’ve ever read, but if you’re working with run loops, it’s a good place to start.

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

Sidebar

Related Questions

We just recently migrated our web application from .NET 1.1 to .NET 2.0. The
I just recently heard of duck typing and I read the Wikipedia article about
I just recently read about Mocking objects for unit testing and currently I'm having
Just recently learned the correct application of inheritance of classes in Java and thought
Just recently I came over an idea called the Application Strangler Pattern . As
I just recently started using Mono for Android and I am trying to consume
I just recently have started learning about the wonders of **kwargs but I've hit
I just recently downloaded the new version of XNA, and trying to follow a
I just recently move to Xcode 4, now I'm stuck trying to find the
I just recently inherited a Rails application and am debating an architectural decision moving

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.