1

I have a standard asynchronous NSURLConnection operation and NSMutableData that loads json from a web service.

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse       *)response
{
     _responseData = [[NSMutableData alloc] init];
}

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


- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
   NSError *error;
   NSDictionary *response = [NSJSONSerialization JSONObjectWithData:_responseData                                                           
                                                            options:NSJSONReadingAllowFragments
                                                              error:&error];

    _responseJson = response;

}

Most of the time it works flawlessly, but for json payloads over an undetermined size I get Cocoa Error 3840 (I have verified the validity of the json). Interestingly when I NSLog the raw data in

-connectionDidFinishLoading: 

It doesn't log all at once and while the logic in the method continues to execute, the raw data continues to load in the console in a staggered fashion.

 -stringFromData:encoding:

Also returns nil while the console logs staggered output. The actual payload is only ~.25mb. I have tried [[NSMutableData alloc] initWithCapacity:] with the same result.

  • Try allocating _responseData before you start downloading. You should clear the data in didReceiveResponse, though, in order to handle redirects and make sure to check/log errors by checking statusCode first (after casting to NSHTTPURLResponse). – EricS May 16 '13 at 01:14
  • Allocating _responseData before initiating the connection makes no difference than in didReceiveResponse. I removed the response handling logic from the posted sample for the sake of clarity. – The Zaporozhian May 16 '13 at 16:04
  • Found the problem. http://stackoverflow.com/questions/12842481/nsjsonserialization-results-in-exc-bad-access/12843465#12843465 – The Zaporozhian Jun 06 '13 at 14:53

0 Answers0