我可以在iOS上使用NSURLSessionDownloadTask进行HTTP缓存吗? [英] Can I use HTTP caching with an NSURLSessionDownloadTask on iOS?

查看:167
本文介绍了我可以在iOS上使用NSURLSessionDownloadTask进行HTTP缓存吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 NSURLSessionDownloadTask ,并利用Apple的内置URL缓存功能。使用下面的代码使用 NSURLSessionDataTask 时,我已经成功地使缓存工作了:

I'm trying to use NSURLSessionDownloadTask, and take advantage of Apple's in-built URL caching functionality. I have succeeded in getting the caching to work when using an NSURLSessionDataTask using the code below:

- (void)downloadUsingNSURLSessionDataTask:(NSURL *)url {
    NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request];
    [dataTask resume];
}

- (void)cachedDataTaskTest {
    // This call performs an HTTP request
    [self downloadUsingNSURLSessionDataTask:[NSURL URLWithString:@"http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"]];
    [NSThread sleepForTimeInterval:1];

    // This call returns the locally cached copy, and no HTTP request occurs
    [self downloadUsingNSURLSessionDataTask:[NSURL URLWithString:@"http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"]];
}

但是,我需要执行后台下载,我必须使用NSURLDownloadTask。当我切换到这个时,不会发生缓存行为。

However, I need to perform a background download for which I have to use an NSURLDownloadTask. When I switch to this the caching behaviour does not occur.

- (void)downloadUsingNSURLSessionDownloadTask:(NSURL *)url {
    NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request];
    [downloadTask resume];
}

- (void)cachedDownloadTaskTest {
    // This call performs an HTTP request
    [self downloadUsingNSURLSessionDownloadTask:[NSURL URLWithString:@"http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"]];
    [NSThread sleepForTimeInterval:1];

    // This call also performs an HTTP request
    [self downloadUsingNSURLSessionDownloadTask:[NSURL URLWithString:@"http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"]];
}

此文档表示 NSURLDownloadTasks 不会调用 URLSession:dataTask:willCacheResponse:completionHandler:委托方法,因此您的应用程序无法挂钩缓存生命周期。我的猜测是,这意味着缓存根本不适用于这些任务,但对此并不明确。

This documentation from Apple indicates that NSURLDownloadTasks don't call the URLSession:dataTask:willCacheResponse:completionHandler: delegate method, so it is not possible for your app to hook into the caching life cycle. My guess is that this implies that caching is simply not available for these tasks, but it is not explicit about this.



  1. 对于数据任务 NSURLSession 对象调用委托的 URLSession:dataTask:willCacheResponse :completionHandler:方法。然后你的
    应用程序应决定是否允许缓存。如果你没有
    实现这个方法,默认行为是使用会话配置对象中指定的缓存
    策略。

  1. For a data task, the NSURLSession object calls the delegate’s URLSession:dataTask:willCacheResponse:completionHandler: method. Your app should then decide whether to allow caching. If you do not implement this method, the default behavior is to use the caching policy specified in the session’s configuration object.


任何人都可以确认NSURLSessionDownloadTasks根本不支持缓存吗?是否可以在后台任务中利用Apple的HTTP缓存行为?

Can anyone confirm this hunch that NSURLSessionDownloadTasks simply don't support caching? Is it possible to take advantage of Apple's HTTP caching behaviour in a background task?

推荐答案

NSURLSessionDownloadTask 使用在应用程序进程外执行下载的系统服务(守护程序)执行工作。因此,实际为下载任务调用的委托回调比 NSURLSessionDataTask 的回调更受限制。如网址会话的生命周期中所述,数据任务委托将接收回调以自定义缓存行为,而下载任务委托则不会。

NSURLSessionDownloadTask performs work using a system service (daemon) that performs the download outside your application process. Because of this, the delegate callbacks that actually get invoked for a download task are more limited than those for NSURLSessionDataTask. As documented in Life Cycle of a URL Session, a data task delegate will receive callbacks to customize caching behavior, while a download task delegate will not.

下载任务应该使用 NSURLRequest 指定的缓存策略,并且应该使用由 NSURLSessionConfiguration (如果没有,请提交错误)。默认缓存策略为 NSURLRequestUseProtocolCachePolicy ,默认URL缓存存储是非后台和非短暂配置的共享URL缓存。 URLSession的委托回调:dataTask:willCacheResponse:completionHandler:不是实际发生缓存的好指标。

A download task should use the caching policy specified by the NSURLRequest, and should use the cache storage specified by the NSURLSessionConfiguration (if it does not, file a bug). The default cache policy is NSURLRequestUseProtocolCachePolicy, and the default URL cache storage is the shared URL cache for non-background and non-ephemeral configurations. The delegate callbacks for URLSession:dataTask:willCacheResponse:completionHandler: are not a good indicator of wether caching is actually occurring.

如果使用默认会话配置创建 NSURLSessionDownloadTask ,并且不自定义 NSURLRequest 的缓存策略,则缓存已经发生了。

If you create an NSURLSessionDownloadTask using the default session configuration and do not customize the cache policy of NSURLRequests, caching is already happening.

这篇关于我可以在iOS上使用NSURLSessionDownloadTask进行HTTP缓存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆