sto creando un NSMutableRequest
:NSURLSessionDataTask timeout richieste successive mancanza
self.req = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0];
Il timeout è impostato per essere di 10 secondi, perché non voglio che l'utente di attendere troppo a lungo per ottenere un feedback. Dopo di che ho creare un NSURLSessionDataTask
:
NSURLSessionDataTask *task = [self.session dataTaskWithRequest:self.req completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse * httpResp = (NSHTTPURLResponse *)response;
if (error) {
// this is where I get the timeout
}
else if (httpResp.statusCode < 200 || httpResp.statusCode >= 300) {
// handling error and giving feedback
}
else {
NSError *serializationError = nil;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&serializationError];
}
[task resume];
}
Il problema è il server va in Gateway Timeout e ci vuole un sacco di tempo. Ottengo l'errore di timeout e invio un feedback all'utente, ma tutte le seguenti chiamate API hanno esito negativo nello stesso modo a causa di un errore di timeout. L'unico modo per fermarlo è quello di uccidere l'app e ricominciare da capo. C'è qualcosa che dovrei fare per uccidere l'attività o la connessione dopo un errore di timeout? Se non imposto un timeout e aspetto di ricevere il codice di errore dal server tutte le seguenti chiamate funzionano perfettamente (ma l'utente aspetta molto!).
ho cercato di annullare l'attività:
NSURLSessionDataTask *task = [self.session dataTaskWithRequest:self.req completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse * httpResp = (NSHTTPURLResponse *)response;
if (error) {
// this is where I get the timeout
[task cancel];
}
...
[task resume];
}
immagino, si potrebbe richiedere di ri-inizializzare il 'self.req' se c'è un errore/timeout. Non sono sicuro di questo però. – Mrunal
prova a chiamare le connessioni con l'app POSTMAN in chrome, e vedi se ottieni ancora l'errore di timeout o no –
Stai creando questa attività all'interno di qualche blocco? O qualche altro thread, diverso dal thread principale? –