我正在使用第三方api进行json响应,我也使用cachePolicy:NSURLRequestReturnCacheDataElseLoad
来快速响应,但是现在我们已经开发了自己的api但现在cachePolicy:NSURLRequestReturnCacheDataElseLoad
无法正常工作,我的意思是它不是缓存数据
NSString *urlAsString = @"api url";
NSString* webStringURL = [urlAsString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:webStringURL];
NSURLRequest *urlRequest =
[NSURLRequest
requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:30.0f];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection
sendAsynchronousRequest:urlRequest
queue:queue
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error) {
if ([data length] >0 && error == nil){
NSMutableArray *arrayResponse=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
[self prepareData:arrayResponse];
}
else if ([data length] == 0 && error == nil){
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:@"The server is not responding \n please check your internet connection" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
alert=nil;
});
NSLog(@"Nothing was downloaded.");
}
else if (error != nil){
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:@"The server is not responding \n please check your internet connection" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
alert=nil;
});
NSLog(@"Error happened = %@", error);
}
}];