NSURL - Are Cached Resource Values Really Automatically Removed After Each Pass Through the Run Loop?

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.

https://developer.apple.com/documentation/foundation/nsurl/removeallcachedresourcevalues()?language=objc

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.

Thanks for the post, I must say, extremely interesting as this is something I have always keep an eye on.

Based on my experience and the documentation provided, it seems there may explain how the API work, however, there are other layers on the request itself that may affect the results in my modest opinion.

Due the request by iOS and the server cache the user may see some discrepancies or additional nuances in how and caching behave, particularly regarding stale values.

While the documentation states that caches values automatically and purges them at the end of each run loop, your observation suggests that cached resource values can persist across run loops unless explicitly cleared, the automatic cache clearing behavior may not always function as expected there might be differences in how cached values are handled depending on the system and the server changes?

Explicit cache management is recommended, especially when dealing with URLs that may have changed state (e.g., deleted or modified), to ensure your application behaves predictably across different environments and runtime sessions.

However the best thing you can do it to create a focused simple project that can add different URL and test those against servers as well as adding different modifiers for URLs like ?t=123456 That may help you to see how it works and how those behaves, maybe the documentation needs a change after your findings?

Looking forward to see how far you get with this one.

Albert Pascual
  Worldwide Developer Relations.

NSURL - Are Cached Resource Values Really Automatically Removed After Each Pass Through the Run Loop?
 
 
Q