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

  • Home
  • SEARCH
  • 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 8217329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:25:26+00:00 2026-06-07T12:25:26+00:00

In Apple’s MVCNetworking sample code, the NetworkManager class includes this method to maintain a

  • 0

In Apple’s MVCNetworking sample code, the NetworkManager class includes this method to maintain a run loop in a secondary thread dedicated to network activity (in order to run NSURLConnection asynchronously):

- (void)networkRunLoopThreadEntry
{
    while(YES) {
        NSAutoreleasePool *pool;
        pool = [[NSAutorelease alloc] init];
        [[NSRunLoop currentRunLoop] run];
        [pool drain];
    }
}

Since the run method exits immediately if there is no source attached to the run loop, this looks like an infinite while loop which is going uselessly to consume CPU resources if there is currently no NSURLConnection attached to the run loop.

On the other hand, to keep the run loop active, some suggests to schedule an empty port in the run loop:

 - (void)networkRunLoopThreadEntry
 {
     NSAutoreleasePool *pool = [[NSAutorelease alloc] init];
     NSPort *port = [NSPort port];
     [[NSRunLoop currentRunLoop] addPort:port forMode:NSRunLoopCommonModes];
     [NSRunLoop run];
     [pool drain];
 }

However, in this case, my worry is that the run method will never exit, which means the pool will never get drained, which means all objects allocated and autoreleased in the secondary thread will leak.

What is the way to go then?

(For the context, as many others, I’m trying to encapsulate an asynchronous NSURLConnection inside a NSOperation, which means it can be triggered outside of the main thread. Also, the MVCNetworking sample code, as well as the WWDC 2010 sessions Network Apps for iPhone OS, seem to suggest it is a good idea to have a unique secondary thread dedicated to network transfers to prevent latency on the main thread.)

  • 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-07T12:25:27+00:00Added an answer on June 7, 2026 at 12:25 pm

    You can create a CFRunLoopObserver for the kCFRunLoopBeforeWaiting activity and add it to the run loop. In the observer’s callout, release the old pool and create a new one. Untested example:

    static void resetPoolCallout(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info) {
        NSAutoreleasePool **poolPointer = (NSAutoreleasePool **)info;
        [*poolPointer release];
        *poolPointer = [[NSAutoreleasePool alloc] init];
    }
    
    - (void)networkRunLoopThreadEntry {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        NSPort *port = [NSPort port];
        [[NSRunLoop currentRunLoop] addPort:port forMode:NSRunLoopCommonModes];
    
        CFRunLoopObserverContext observerContext = {
            .version = 0,
            .info = (void*)&pool,
            .retain = NULL,
            .release = NULL,
            .copyDescription = NULL
        };
        CFRunLoopObserverRef observer = CFRunLoopObserverCreate(NULL, kCFRunLoopBeforeWaiting,
            true, 0, resetPoolCallout, &observerContext);
        CFRunLoopAddObserver(CFRunLoopGetCurrent(), observer, kCFRunLoopCommonModes);
    
        [[NSRunLoop currentRunLoop] run];
    
        CFRunLoopRemoveObserver(CFRunLoopGetCurrent(), observer, kCFRunLoopCommonModes);
        CFRelease(observer);
    
        [pool release];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apple just released some sample code on lazy loading images in a UITableView a
Apple is doing this in a setter method for an instance variable mainSprocket: –
Apple used to have EditableDetailView as sample code to learn how to do editable
Apple.h class Apple { public: Apple(int); static int typeID; private: int id_; }; Apple.cpp
Apple introduced Grand Central Dispatch (a thread pool) in Snow Leopard, but haven't gone
Apple's Technical Q&A on addressing flickering (QA1650) includes the following paragraph. (Emphasis mine.) You
Apple tends to give examples like this: NSError __strong *error = nil; or -(BOOL)performOperationWithError:(NSError
Apple seems to say that this image should have no extension. But how is
Apple is always in the news these days with i this and i that.
Apple's multithreading docs don't list NSIndexPath as threadsafe or not! As an immutable class,

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.