失败后重试请求

时间:2014-05-11 07:16:41

标签: ios networking

我正在使用app提供的默认类来在我的应用中执行网络任务。

在某些时候,我的一些请求超时(无论超时的原因)

我想重试其中一些请求

我已经将NSURLConnection类子类化,并为其添加了一些参数。

但是,在方法“connection:didFailWithError:”

con.retries似乎总是1,从不递增。为什么?

- (void)connection:(NSURLConnection*) connection didFailWithError:(NSError *)error
{

[[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
NSLog(@"%@",[NSString stringWithFormat:@"Did recieve error: %@", [error description]]);
NSLog(@"%@",[NSString stringWithFormat:@"%@", [[error userInfo]description]]);
WBURLConnection *con = (WBURLConnection *)connection;
if([con shouldRetryRequest:error]){
    con.retries ++;
    [con start];
}else{
    [con cancel];
    [con.data  setLength:0];
    if(!self.alert){
        self.alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [self.alert show];
    }else {
        if(![self.alert isVisible]){
            [self.alert show];
        }
    }

}

}

-(BOOL)shouldRetryRequest:(NSError *)error{
    [self.retryCount appendString:[NSString stringWithFormat:@"%@:%ld",error,(long)self.retries]];
    LogInfo(@"retries:%@",self.retryCount);
    if([error code] == -1004){
        return NO;
    }
    return self.retries<3;
}

0 个答案:

没有答案