0

I am getting response data from a server using AFHTTPRequestOperation in AfNetworking 2.0

NSURLRequest *request = [[ServiceHelper instance] getRequestData:postDict :[ServicesConfiguration GET_DOCUMENTS_URL]];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];

        AFHTTPRequestOperation *requestOperation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {

            NSDictionary *returnData = [[ServiceHelper instance] getReturnDictionary:responseObject];

        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error: %@", error);
        }];

The getReturnDictionary method called on the responseObject is a simple JSON Serializer..

- (NSDictionary *) getReturnDictionary : (NSData *) data {

    if ( data == nil ) {
        return [NSDictionary dictionary];
    }

    NSError * error = nil;
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

    if (error != nil) {
        NSLog(@"Error parsing JSON: %@",error);
        return [NSDictionary dictionary];
    }
    else
        return jsonDict;
}

This works fine for smaller amounts of data. But when the response object is like 100mb, the app hangs on

NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

And then about 15 seconds later, the app crashes due to a memory error. I think its pretty self explanatory that its the massive size of the data, but shouldn't it be able to handle it?

If I get the data directly from an [ NSURLConnection sendSynchronousRequest:]; - it works without hanging or crashing. As this is what I was doing originally - but switching to AFNetworking to display a progress bar more easily.

Any thoughts or tips are appreciated.

nserror
  • 727
  • 8
  • 19

1 Answers1

0

Update: So You have 2 options to solve this:

Blockquote

Use NSJSONReadingMutableContainers as options

Blockquote

If previous does not work you are facing a known issue like below:

iOS Download & Parsing Large JSON responses is causing CFData (store) leaks

So now you have 2 options:

  1. Use native JSON Serialization
  2. First downloading the JSON files to disk without using AFNetworking and than parse.
Community
  • 1
  • 1
bllakjakk
  • 5,045
  • 1
  • 18
  • 28