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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:40:21+00:00 2026-06-04T23:40:21+00:00

I’ve been having intermittent problems with NSURLConnection requests timing out in our iPhone app.

  • 0

I’ve been having intermittent problems with NSURLConnection requests timing out in our iPhone app. It seems to be occurring more of late. Once it enters this state, it stays in that state. The only resolution seems to be killing the app and restarting it.

Observations:

  • The core code that executes the NSURLConnection has not changed (except for some custom user-agent code recently added).
  • Have yet to find a reproducible case, but timeouts seem to occur after the app has been sitting in the background for a while, particularly if running on 3G (no WiFi).
  • Apache on server is logging no requests from client while it’s experiencing these timeouts.
  • Some indications that other apps, like Mail and Safari are affected (i.e., experiencing timeouts), although not consistently.
  • 3G coverage is solid where I’m at, not to rule out a transitory issue triggering the problem (assumed not likely).
  • All requests are going to our own API server, and are restful POST requests.
  • We use our own NSTimer-based timeout, due to the issues with timeoutInterval and POST requests. I’ve tried playing around with increasing the timeout value — problem still occurs.

Other miscellaneous stuff:

  • App was recently converted to ARC.
  • Running app under iOS 5.1.1.
  • App uses latest versions of UrbanAirship, TestFlight and Flurry SDKs.
  • Also using ARC branch of TouchXML to parse responses.

As you can see below, the code runs on the main thread. I assumed something is blocking on that thread, but the stack traces I see when suspending the app suggest the main thread is fine. I take it that NSURLConnection is using its own thread and that must be blocked.

#define relnil(v) (v = nil)

- (id) initWebRequestController
{
    self = [super init];
    if (self)
    {
        //setup a queue to execute all web requests on synchronously
        dispatch_queue_t aQueue = dispatch_queue_create("com.myapp.webqueue", NULL);
        [self setWebQueue:aQueue];
    }
    return self;
}

- (void) getStuffFromServer
{
    dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(aQueue, ^{
        dispatch_sync([self webQueue], ^{            
            error_block_t errorBlock = ^(MyAppAPIStatusCode code, NSError * error){
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[self delegate] webRequestController:self didEncounterErrorGettingPointsWithCode:code andOptionalError:error];
                });
            };

            parsing_block_t parsingBlock = ^(CXMLDocument * doc, error_block_t errorHandler){
                NSError * error = nil;

                CXMLNode * node = [doc nodeForXPath:@"apiResult/data/stuff" error:&error];
                if (error || !node) {
                    errorHandler(MyAppAPIStatusCodeFailedToParse, error);
                }
                else {
                    stuffString = [node stringValue];
                }

                if (stuffString) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [[self delegate] webRequestController:self didFinishGettingStuff:stuffString];
                    });
                }
                else {
                    errorHandler(MyAppAPIStatusCodeFailedToParse, error);
                }
            };

            NSURL * url = [[NSURL alloc] initWithString:[NSString stringWithFormat:MyAppURLFormat_MyAppAPI, @"stuff/getStuff"]];

            NSMutableURLRequest * urlRequest = [[NSMutableURLRequest alloc] initWithURL:url];
            NSMutableDictionary * postDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                                    [[NSUserDefaults standardUserDefaults] objectForKey:MyAppKey_Token], @"token",
                                                    origin, @"from",
                                                    destination, @"to",
                                                    transitTypeString, @"mode",
                                                    time, @"time",
                                                    nil];

            NSString * postString = [WebRequestController httpBodyFromDictionary:postDictionary];
            [urlRequest setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
            [urlRequest setHTTPMethod:@"POST"];

            if (urlRequest)
            {
                [self performAPIRequest:urlRequest withRequestParameters:postDictionary parsing:parsingBlock errorHandling:errorBlock timeout:kTimeout_Standard];
            }
            else
            {
                errorBlock(MyAppAPIStatusCodeInvalidRequest, nil);
            }

            relnil(url);
            relnil(urlRequest);
        });
    });
}

- (void) performAPIRequest: (NSMutableURLRequest *) request
     withRequestParameters: (NSMutableDictionary *) requestParameters
                   parsing: (parsing_block_t) parsingBlock 
             errorHandling: (error_block_t) errorBlock
                   timeout: (NSTimeInterval) timeout
{
    NSAssert([self apiConnection] == nil, @"Requesting before previous request has completed");

    NSString * postString = [WebRequestController httpBodyFromDictionary:requestParameters];
    [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

    NSString * erVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    NSString * erBuildVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    if ([erBuildVersion isEqualToString:erVersion] || [erBuildVersion isEqualToString:@""]) {
        erBuildVersion = @"";
    } else {
        erBuildVersion = [NSString stringWithFormat:@"(%@)", erBuildVersion];
    }
    NSString * iosVersion = [[UIDevice currentDevice] systemVersion];
    NSString * userAgent = [NSString stringWithFormat:@"MyApp/%@%@ iOS/%@", erVersion, erBuildVersion, iosVersion];
    [request setValue:userAgent forHTTPHeaderField:@"User-Agent"];

    [request setTimeoutInterval:(timeout-3.0f)];

    dispatch_sync(dispatch_get_main_queue(), ^{
        NSURLConnection * urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];

        if (urlConnection)
        {
            [self setApiConnection:urlConnection];

            requestParseBlock = [parsingBlock copy];
            requestErrorBlock = [errorBlock copy];

            NSMutableData * aMutableData = [[NSMutableData alloc] init];
            [self setReceivedData:aMutableData];
            relnil(aMutableData);

            [urlConnection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];

            [urlConnection start];
            relnil(urlConnection);

            NSTimer * aTimer = [NSTimer scheduledTimerWithTimeInterval:timeout target:self selector:@selector(timeoutTimerFired:) userInfo:nil repeats:NO];
            [self setTimeoutTimer:aTimer];
        }
        else
        {
            errorBlock(MyAppAPIStatusCodeInvalidRequest, nil);
        }
    });

    //we want the web requests to appear synchronous from outside of this interface
    while ([self apiConnection] != nil)
    {
        [NSThread sleepForTimeInterval:.25];
    }
}

- (void) timeoutTimerFired: (NSTimer *) timer
{
    [[self apiConnection] cancel];

    relnil(apiConnection);
    relnil(receivedData);

    [self requestErrorBlock](MyAppAPIStatusCodeTimeout, nil);

    requestErrorBlock = nil;
    requestParseBlock = nil;
}


- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{    
    [self requestErrorBlock](MyAppAPIStatusCodeFailedToConnect, error);

    relnil(apiConnection);
    relnil(receivedData);
    [[self timeoutTimer] invalidate];
    relnil(timeoutTimer);
    requestErrorBlock = nil;
    requestParseBlock = nil;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [receivedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{    
    MyAppAPIStatusCode status = MyAppAPIStatusCodeFailedToParse;

    CXMLDocument *doc = [[self receivedData] length] ? [[CXMLDocument alloc] initWithData:[self receivedData] options:0 error:nil] : nil;

    DLog(@"response:\n%@", doc);

    if (doc)
    {
        NSError * error = nil;
        CXMLNode * node = [doc nodeForXPath:@"apiResult/result" error:&error];
        if (!error && node)
        {
            status = [[node stringValue] intValue];

            if (status == MyAppAPIStatusCodeOK)
            {
                [self requestParseBlock](doc, [self requestErrorBlock]);
            }
            else if (status == MyAppAPIStatusCodeTokenMissingInvalidOrExpired)
            {
                [Definitions setToken:nil];

                [self requestMyAppTokenIfNotPresent];

                [Definitions logout];

                dispatch_async(dispatch_get_main_queue(), ^{
                    [[self delegate] webRequestControllerDidRecivedExpiredTokenError:self];
                });
            }
            else
            {
                [self requestErrorBlock](status, nil);                
            }
        }
        else
        {
            [self requestErrorBlock](status, nil);
        }
    }
    else
    {
        status = MyAppAPIStatusCodeUnexpectedResponse;
        [self requestErrorBlock](status, nil);
    }
    relnil(doc);

    relnil(apiConnection);
    relnil(receivedData);
    [[self timeoutTimer] invalidate];
    relnil(timeoutTimer);
    requestErrorBlock = nil;
    requestParseBlock = nil;
}

URLs below are some screenshots of the queues/threads when the app was in the problematic state. Note, I believe thread 10 is related to the cancel performed on the previous timeout, although the mutex wait is curious. Also, the bit in thread 22 about Flurry does not consistently appear when experiencing the problem on other occasions.

Stack trace screenshots:

http://img27.imageshack.us/img27/5614/screenshot20120529at236.png
http://img198.imageshack.us/img198/5614/screenshot20120529at236.png

Perhaps I’m overlooking something obviously wrong in those traces, as I’m relatively new to iOS/Apple development.

All of this would be much simpler to solve if I had the source for NSURLConnection and related code, but such as it is, I’m taking stabs in the dark at this point.

  • 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-04T23:40:23+00:00Added an answer on June 4, 2026 at 11:40 pm

    Removing the TestFlight 1.0 SDK seemed to fix the problem. TestFlight also confirmed that they’re working on a fix. Given that it’s been over a month since the bug was originally confirmed by others, I wonder how close we are to getting a fix.

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

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am writing an app with both english and french support. The app requests
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm having trouble keeping the paragraph square between the quote marks. In firefox the

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.