ios - 如果我在一个 View Controller 中调用异步操作并且用户移动到下一个 View Controller ,异步调用是否完成

标签 ios afnetworking afnetworking-2 nsoperationqueue sdwebimage

在我的第一个屏幕上,我想加载和缓存多个 JSON 调用和图像调用。如果用户在完成之前单击第二个 View Controller , 这些电话会被取消还是会结束?

AFNetworking 中的示例调用:

  AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
  manager.responseSerializer = [AFJSONResponseSerializer serializer];
  manager.requestSerializer = [AFJSONRequestSerializer serializer];
  NSString *arcURL = [NSString stringWithFormat:@"%@/arc/v1/api/locations/%i/mobile_home",MYHost(), locationID];
  [manager GET:arcURL parameters:nil success:^(NSURLSessionDataTask *operation, id responseObject) {

  // on return will get written to a cache

SDWebImage 中的示例调用

    NSURL *imageURL=[[NSURL alloc] initWithString:[mi objectForKey:@"instore_image_url"]];
  SDWebImageManager *manager = [SDWebImageManager sharedManager];
  [manager downloadWithURL:imageURL
                   options:0
                  progress:^(NSInteger receivedSize, NSInteger expectedSize)
   {
     // progression tracking code
   }
                 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
   {
     if (image)
     {
        // image downloaded - just writing to cache
     }
   }];
}

最佳答案

GET , POST等等,AFHTTPSessionManager的方法返回NSURLSessionTask引用。如果您离开发起请求的 Controller ,请求将继续,除非您 (a) 保存对该 NSURLSessionTask 的引用。目的; (b) 显式调用cancel该对象的方法。

SDWebImage方法downloadWithURL同样返回一个引用(在本例中是一个符合 SDWebImageOperation 协议(protocol)的对象),您可以 cancel如果你愿意的话。

关于ios - 如果我在一个 View Controller 中调用异步操作并且用户移动到下一个 View Controller ,异步调用是否完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27513739/

相关文章:

ios - AFNetworking 下载队列

ios - 使用通用协议(protocol)作为接口(interface)

ios - 如何访问 UIColor 的 RGB 值?

ios - 通过 CocoaPods 集成 Fabric/Crashlytics

heroku - 未使用 AFNetworking 发送授权 header

ios - 如何在 AFNetworking 2 中执行 AFHTTPRequestOperation

ios - URL AFNetworking GET 中的变音符号

ios - 如何在 View 出现之前滚动到 iPhone 上 UITableView 的底部

ios - 将 View Controller 作为参数传递给 AFNetworking 请求是否安全?

ios - 使用 AFNetworking 在 IOS 应用程序中集成 SSL 证书