NSUrlsession 被触发,url 没有被调用,但是结果
NSUrlsession fired, url not called, nevertheless result
我有这个方法可以从 URL 中获取 JSON 数据:
-(void)getJsonResponse:(NSString *)urlStr success:(void (^)(NSDictionary *responseDict))success failure:(void(^)(NSError* error))failure
{
NSURLSession *session = [NSURLSession sharedSession];
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//NSLog(@"%@",data);
if (error) {
failure(error);
NSLog(@"Error: %@", [error localizedDescription]);
}
else {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
//NSLog(@"%@",json);
success(json);
}
}];
[dataTask resume];
}
在 myViewController 中,viewWillAppear 我这样调用这个方法:
NSString * URLString = @"my.valid.url";
[self getJsonResponse:URLString success:^(NSDictionary *result) {
//here some code when succesful
} failure:^(NSError *error) {
NSLog(@"Something terrible happened");
}];
}
效果很好,但只有一次:
当我离开 myViewController 并再次进入时,
- viewWillAppear 被调用,随后
- [自己getJsonResponse:...被调用
- 我在成功块中的代码被执行
但是:我注意到与 Charles 一起监控网络 activity,没有调用 my.valid.url。
什么给了?我应该使共享会话无效吗?如果是这样,什么时候?
设置 NSURLSessionConfiguration chachePolicy to NSURLRequestReloadIgnoringCacheData
and try again. Here is the good resource 以了解 Http 缓存。阅读苹果指南中给出的文档。
NSURLSessionConfiguration *config = NSURLSessionConfiguration.defaultSessionConfiguration;
config.requestCachePolicy = NSURLRequestReloadIgnoringCacheData;
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
我有这个方法可以从 URL 中获取 JSON 数据:
-(void)getJsonResponse:(NSString *)urlStr success:(void (^)(NSDictionary *responseDict))success failure:(void(^)(NSError* error))failure
{
NSURLSession *session = [NSURLSession sharedSession];
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//NSLog(@"%@",data);
if (error) {
failure(error);
NSLog(@"Error: %@", [error localizedDescription]);
}
else {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
//NSLog(@"%@",json);
success(json);
}
}];
[dataTask resume];
}
在 myViewController 中,viewWillAppear 我这样调用这个方法:
NSString * URLString = @"my.valid.url";
[self getJsonResponse:URLString success:^(NSDictionary *result) {
//here some code when succesful
} failure:^(NSError *error) {
NSLog(@"Something terrible happened");
}];
}
效果很好,但只有一次:
当我离开 myViewController 并再次进入时,
- viewWillAppear 被调用,随后
- [自己getJsonResponse:...被调用
- 我在成功块中的代码被执行
但是:我注意到与 Charles 一起监控网络 activity,没有调用 my.valid.url。
什么给了?我应该使共享会话无效吗?如果是这样,什么时候?
设置 NSURLSessionConfiguration chachePolicy to NSURLRequestReloadIgnoringCacheData
and try again. Here is the good resource 以了解 Http 缓存。阅读苹果指南中给出的文档。
NSURLSessionConfiguration *config = NSURLSessionConfiguration.defaultSessionConfiguration;
config.requestCachePolicy = NSURLRequestReloadIgnoringCacheData;
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];