有没有办法在 watchOS 中使用 `useProtocolCachePolicy`?

Is there a way to use `useProtocolCachePolicy` in watchOS?

我正在尝试在我的手表扩展中使用 URLRequest.CachePolicy.useProtocolCachePolicy,但每个请求都失败并出现 'can’t load from network' 错误。

我看到了下面的回答:

但我想做的是使用缓存(如果有效)或请求策略。最合适的是使用 reloadRevalidatingCacheData 但尚未实现。

@constant NSURLRequestReloadRevalidatingCacheData Specifies that the existing cache data may be used provided the origin source confirms its validity, otherwise the URL is loaded from the origin source. Unimplemented.

在 iOS 中,useProtocolCachePolicyreloadRevalidatingCacheData 的工作方式非常相似。

https://developer.apple.com/documentation/foundation/nsurlrequest/cachepolicy/useprotocolcachepolicy

但是使用 useProtocolCachePolicy 策略在 watchOS 中请求失败。顺便说一下,它与 reloadIgnoringLocalCacheData 一起工作。

我没有发出 HTTP 或 HTTPS 字节范围请求,因此 useProtocolCachePolicy 似乎像 iOS 一样工作,但事实并非如此。

唯一的方法似乎是手动实现缓存,但我想知道为什么它不起作用,并且有任何开箱即用的选项可以解决这个问题。

我从 Apple 那里得到了关于这个问题的答复。

Watch apps tend to be suspended very quickly so we recommend that developers use a background url session to ensure their api calls are still performed should an event such as backgrounding or suspension occur.

如果有人想使用 .useProtocolCachePolicy 策略,请使用后台配置。

let configuration = URLSessionConfiguration.background(withIdentifier: "xxx.xxx.xxxxx")
let session = URLSession(configuration: configuration)