Answer the question
In order to leave comments, you need to log in
How to save data correctly when sending multiple GET requests?
Hello! Comrades, help me out!
In general, I have several GET requests. I can not correctly save the data when parsing. Can you please tell me the best way to save?
Tried via dispatch_async but no selenium in that (nothing worked).
I save in homesJSON and roomsJSON.
First did this.
#pragma mark Processing the received response
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
NSInteger responseStatusCode = [httpResponse statusCode];
NSLog(@"Код сервера = %li", (long)responseStatusCode);
// получен ответ от сервера
[_receivedData setLength:0]; // очистка ранее полученных данных
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// добавляем новые данные к receiveData
[_receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// выполнение операций с полученными данными
NSLog(@"Обработка полученных данных");
_resultResponse = [[NSString alloc] initWithData:_receivedData encoding:NSUTF8StringEncoding];
NSLog(@"dataStr = %@", _resultResponse);
_homesJSON = [HomesModel arrayOfModelsFromString:_resultResponse error:nil];
if (_homesJSON)
NSLog(@"Data homes saved.");
_roomsJSON = [RoomsModel arrayOfModelsFromString:_resultResponse error:nil];
if (_roomsJSON)
NSLog(@"Data rooms saved.");
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^
{
[self getRequestHomes];
_homesJSON = [HomesModel arrayOfModelsFromString:_resultResponse error:nil];
if (_homesJSON)
NSLog(@"Data homes saved.");
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
{
[self getRequestRooms];
_roomsJSON = [RoomsModel arrayOfModelsFromString:_resultResponse error:nil];
if (_roomsJSON)
NSLog(@"Data rooms saved.");
});
Answer the question
In order to leave comments, you need to log in
Have you tried just using NSURLSession? and NSURLSessionDataTask -
- dataTaskWithRequest:completionHandler: and in the completionHandler block all the necessary parsing
In theory, this should not work at all, because. and the NSURLConnection itself is asynchronous, so you still do an asynchronous dispatch. I would immediately in the dispatcher, that's what I would do
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^
{
NSString *_resultResponse = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"https://blabla.com/user?id=1"] usedEncoding:NSUTF8StringEncoding error:nil];
_homesJSON = [HomesModel arrayOfModelsFromString:_resultResponse error:nil];
if (_homesJSON)
NSLog(@"Data homes saved.");
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question