1

I am trying to download a JSON with NSURLConnection, but unless I force the app to pause some seconds the data I get isn't complete. It is always around 2600 bytes and my response should be around 70000.

Any clue why is this happening?

Thank You

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    _responseData = [[NSMutableData alloc] init];
    //sleep(10);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [_responseData appendData:data];
    [self getDataJSON: _responseData];
}
Wain
  • 118,658
  • 15
  • 128
  • 151
Andoxko
  • 1,021
  • 2
  • 12
  • 19
  • Did receive data will be call every time when data will come back from server. Data can come in packets, so you may not receive full. You will have full data when you didFinishLoading method will execute. You should have a instance variable and append data every time when didReceive execute. – Adnan Aftab Nov 20 '13 at 12:00

2 Answers2

4

didReceiveData is called a lot of times, while it is receiving data

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

}

You have to wait until it finish receiving data

- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
     [self getDataJSON: _responseData];
}
jcesarmobile
  • 51,328
  • 11
  • 132
  • 176
0

You can get complete data when connection is finished. NSURLConnection finishes when it's connectionDidFinishLoading: delegate method is called. Try [self getDataJSON: _responseData]; in that method. Good Luck!

Fahri Azimov
  • 11,470
  • 2
  • 21
  • 29
  • Thank you very much!! You are completely right also! Sorry about not accepting your answer :( I can´t accept 2 answers and he answers first. – Andoxko Nov 20 '13 at 12:06