如何重试基于块的URL请求 [英] How to retry a block based URL Request
问题描述
我使用iOS7的新网址请求方法获取数据,如下所示:
NSMutableURLRequest * request = [NSMutableURLRequest
),并需要向服务器重新发送相同的请求。
requestWithURL:[NSURL URLWithString:[self.baseUrl
stringByAppendingString:path]]];
NSURLSessionDataTask * dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * data,NSURLResponse * response,NSError * error){
NSHTTPURLResponse * httpResponse =(NSHTTPURLResponse *)response ;
NSUInteger responseStatusCode = [httpResponse statusCode];
if(responseStatusCode!= 200){
// RETRY($)
} else
completionBlock(results [@result] [符号]);
}];
[dataTask resume];不幸的是,我不时得到HTTP响应,表明服务器不可达(<$ c $ b c> response code!= 200
如何做到这一点?我需要完成上面的代码片段,其中我的注释
// RETRY
是?
成功提取后调用完成块。
但是如何再次发送同样的请求?
谢谢!
解决方案将请求代码放入方法中,并在
dispatch_async
块中再次调用它;)- (void)requestMethod {
NSMutableURLRequest * request = [NSMutableURLRequest
requestWithURL:[NSURL URLWithString:[self.baseUrl
stringByAppendingString :路径]]];
__weak typeof(self)weakSelf = self;
NSURLSessionDataTask * dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * data,NSURLResponse * response,NSError * error){
NSHTTPURLResponse * httpResponse =(NSHTTPURLResponse *)response;
NSUInteger responseStatusCode = [httpResponse statusCode];
if(responseStatusCode!= 200){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0ul),^ {
[weakSelf requestMethod];
});
} else
completionBlock(results [@result] [symbol]);
}];
[dataTask resume];
}
I am fetching data using iOS7's new URL request methods, like so:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[self.baseUrl stringByAppendingString:path]]]; NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response; NSUInteger responseStatusCode = [httpResponse statusCode]; if (responseStatusCode != 200) { // RETRY (??????) } else completionBlock(results[@"result"][symbol]); }]; [dataTask resume];
Unfortunately, from time to time I get HTTP responses indicating the server is not reachable (
response code != 200
) and need to resend the same request to the server.How can this be done? How would I need to complete my code snippet above where my comment
// RETRY
is?In my example I call the completion block after a successful fetch. But how can I send the same request again?
Thank you!
解决方案Put your request code in a method and call it again in a
dispatch_async
block ;)- (void)requestMethod { NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[self.baseUrl stringByAppendingString:path]]]; __weak typeof (self) weakSelf = self; NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response; NSUInteger responseStatusCode = [httpResponse statusCode]; if (responseStatusCode != 200) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{ [weakSelf requestMethod]; }); } else completionBlock(results[@"result"][symbol]); }]; [dataTask resume]; }
这篇关于如何重试基于块的URL请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!