How do I know if the app's Web Service is down?

1

Scenario: The app consumes data from a web service, so that the app is not "locked" I added the task of downloading the data in a secondary trhead, according to the following code:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  //realize aqui o trabalho em background
        dispatch_async(dispatch_get_main_queue(), ^{ 
       //quando as operações em background forem concluídas, execute aqui o código na thread principal para atualização da tela, caso necessário
        });
    });

However, when the web service is down, the app is waiting for the server to respond, and after a while without the response, iOS terminates my app.

I would like to know if there is a Design Pattern for this type of situation, where we can control how long the app will wait for the server to respond, and tell iOS to close the data request process without having to close the app !

    
asked by anonymous 02.11.2014 / 13:51

1 answer

1

To make requests to the web service, the most used library is AFNetworking. It is easy to handle situations in which the request fails, whether due to connection or server problems. The library is already in charge of performing the operations in the background, managing multiple requisitions, among other things.

To use it, add Podfile pod "AFNetworking" to it. If you do not use Cocoapods yet, read the link .

Each request will always have a success block, called if the request completes successfully, and an error block, called when the request fails (for example due to a timeout in the operation). Example:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
    
02.11.2014 / 21:03