Ios AFHTTPRequestOperationManager针对死机服务器调用成功块

Ios AFHTTPRequestOperationManager针对死机服务器调用成功块,ios,afnetworking-2,Ios,Afnetworking 2,我找不到一个问题来回答这个问题。这就是 警告:我是iOS和Objective-C新手。我在iOS 7.0应用程序中使用AFNetworking 2.0来执行异步网络请求。代码块如下所示: - (void)requestLoad { AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFJSONRespons

我找不到一个问题来回答这个问题。这就是

警告:我是iOS和Objective-C新手。我在iOS 7.0应用程序中使用AFNetworking 2.0来执行异步网络请求。代码块如下所示:

- (void)requestLoad {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager GET:@"http://localhost:8000/api"
  parameters:nil
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
         NSLog(@"The AFHTTP op returned successfully");
     }
     failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"The AFHTTP op failed");
     }];
}
NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8000/api"]
                                          cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                      timeoutInterval:1.0];
在执行第一个请求(此方法的第一个调用)时,我获得了一个成功的返回日志,但是随后我关闭服务器并通过iOS模拟器执行相同的请求:我仍然获得了一个成功的返回


我猜库或iOS本身中有某种类型的URL缓存,但我一直无法将其关闭。还是我做错了什么?

您是否尝试过清理项目(cmd+shift+k)并重建它

事实证明,您需要使用
requestWithURL:cachePolicy:timeoutInterval:
显式覆盖NSURLRequest缓存行为,因为
requestWithURL:
中的默认值将默认设置
cachePolicy
NSURLRequestUseProtocolCachePolicy
。因此,如果您的服务器指定缓存的最大年限为300,即使在请求之间拆下服务器,NSURLRequest仍将保留原始的300秒缓存

覆盖如下所示:

- (void)requestLoad {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager GET:@"http://localhost:8000/api"
  parameters:nil
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
         NSLog(@"The AFHTTP op returned successfully");
     }
     failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"The AFHTTP op failed");
     }];
}
NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8000/api"]
                                          cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                      timeoutInterval:1.0];
我还必须说“我的坏”,我记得以前读过,在
NSURLRequest
中关闭缓存是通过将cachePolicy设置为0,这已经相当于
NSURLRequestUseProtocolCachePolicy
(请参阅:


解决方案是将其设置为1。

您是否使用NSURLCache设置了缓存?powerj1984-否我从未设置过缓存,我还尝试使用带有NSURLRequest的AFHTTPRequestOperation,我将缓存显式设置为0,设置为no availHi Sean,非常感谢您的回答。是的,我现在已经设置了,但此方法在一次运行期间被多次调用,实际上是使用e我在运行过程中关闭了服务器,点击home按钮,将应用程序重新启动,并看到操作正在成功返回到不应该返回的位置。