The documentation says:
The caching behavior of the NSURL and CFURL APIs differ. For NSURL, all cached values (not temporary values) are automatically removed after each pass through the run loop. You only need to call the removeCachedResourceValueForKey: method when you want to clear the cache within a single execution of the run loop. The CFURL functions, on the other hand, do not automatically clear cached resource values. The client has complete control over the cache lifetimes, and you must use CFURLClearResourcePropertyCacheForKey or CFURLClearResourcePropertyCache to clear cached resource values.
Is this really true? In my experience I've had to explicitly remove cached resource values via -removeAllCachedResourceValues or removeCachedResourceValueForKey: otherwise the URL contains stale values.
For example on a URL that no longer exists I attempted to read NSURLIsHiddenKey and the last value was already cached. Instead of getting a NSFileNoSuchFileError I get the old cache value unless explicitly call -removeCachedResourceValueForKey: first and I'm fairly certain the value was cached on a previous run loop churn.
But whose run loop 'owns' the URL resources?
This question doesn’t make sense. Well, it makes sense given how this feature is documented, but that documentation is aiming to offer a high-level overview of the effect of the feature, not a detailed description of its implementation.
This isn’t implemented using active cache flushing. Rather, it’s based on passively detecting invalidation. When you access a property on the main thread, the system checks to see if the cached value is still valid, that is, has the main thread ‘turned’ between when you last got the property and now.
The exact mechanics of this aren’t documented, and for good reason. It’s easy to imagine how this implementation might change over time. Rather, the take-home message is:
- You have to worry caching.
- If you’re working on the main thread, that worry is bounded by the turning of the run loop.
it doesn't make any sense to me
This design dates from macOS 10.6, which is over 15 years ago, and things have evolved a bit since then.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"