didReceiveData is not getting all data

…衆ロ難τιáo~ 提交于 2019-12-01 11:02:50

问题


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];
}

回答1:


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];
}



回答2:


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!



来源:https://stackoverflow.com/questions/20095234/didreceivedata-is-not-getting-all-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!