V
V
Vladislav Pavlenko2016-02-24 01:10:29
Objective-C
Vladislav Pavlenko, 2016-02-24 01:10:29

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.");
}

Then I tried using dispatch_async.
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

[[+comments_count]] answer(s)
A
Anton Marunko, 2016-02-24
@pavlenkovs

Have you tried just using NSURLSession? and NSURLSessionDataTask -
- dataTaskWithRequest:completionHandler: and in the completionHandler block all the necessary parsing

A
Alexander, 2016-02-24
@alexyat

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.");
                   });

this is in the style of x * yak-x * yak and in production
, and also if you communicate with the server via http, and not via https, then you need to add App Transport Security Settings to Info.plist as in the screenshot
20bc025b23a54105bf1769834669805e.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question