How do I know the app’s web service is down?

Asked

Viewed 181 times

1

Scenario: The app consumes data from a web service, so that the app does not get "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 reply, and after a while without the answer, iOS shuts down my app.

I wonder if there is a Design Pattern for this type of situation, where we can control how long the app will be waiting for server response, and inform iOS to end the data request process without having to close the app!

1 answer

1


For web service requests, the most used library is Afnetworking. It makes it easy to handle situations where the request fails, either because of connection or server problems. The library is already in charge of performing background operations, managing multiple requests, among other things.

To use it, add to Podfile pod "AFNetworking". If you don’t already use Cocoapods read the link.

Each request will always have a success block, called if the request successfully completes, 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);
}];
  • But can I determine the waiting time on the client side? Type determine that the request time to be completed successfully should be at least 30 seconds and then send a Uialert to the user that connection to the web service was not possible?

  • 1

    Yes, you can change the timeout time. Example: manager.requestSerializer.timeoutInterval = 120; The default value is 60 seconds. That is, if the request does not complete in 60 seconds, it will fail. To notify the user about the error, just show the alert in the error block.

  • Thanks again @Rafaelleao I’ll try it and I’ll tell you if it worked!

  • Hello! @Rafaelleao I tried to use this example but returned an error without downloading any data. Can we chat using skype or stackoverflow chat? My Skype > [email protected] Thanks!

  • 1

    I managed to solve the problem... I had to add a code in the Afurlresponseserialization file. m in line 215: ,@"text/html",nil]; In the following method self.acceptableContentTypes = [Nsset setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",nil];

  • It worked! Thank you very much!

  • I have another question because I do not use Afnetworking much, but I will use it from here on, I will post the question, if you can answer already thank you in advance! rs Link to another question: http://answall.com/questions/39332/comort-dataos-para-web-service-usando-afnetworking

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.