My experience is that the cache tends to run async, so if you delete an item and then immediately request an item you get back the original item. You only get back
nil once the delete has run to completion.
You can see this in action with the code below, which doesn’t hit any of thes asserts.
As to whether the cache
should work this way, that’s a more subtle question and you should feel free to make your case in a
bug report.
Please post your bug number, just for the record.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Code Block func testAsync() { |
print("start") |
let url = URL(string: "https://apple.com")! |
request = URLRequest(url: url) |
self.request.httpBody = "test".data(using: .utf8)! |
let httpResponse = HTTPURLResponse(url: url, statusCode: 200, httpVersion: nil, headerFields: nil)! |
self.cachedURLResponse = Foundation.CachedURLResponse(response: httpResponse, data: request.httpBody!) |
let cache = URLCache.shared |
cache.removeAllCachedResponses() |
assert(cache.cachedResponse(for: request) == nil, "Remove all should start clean") |
self.count = 0 |
step1() |
} |
|
var request: URLRequest! |
var cachedURLResponse: CachedURLResponse! |
var count: Int = 0 |
|
func step1() { |
let cache = URLCache.shared |
assert(cache.cachedResponse(for: request) == nil, "[\(count)] Request should be removed") |
cache.storeCachedResponse(cachedURLResponse, for: request) |
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) { |
self.step2() |
} |
} |
|
func step2() { |
let cache = URLCache.shared |
assert(cache.cachedResponse(for: request) != nil, "[\(count)] Request should be found inside the cache") |
cache.removeCachedResponse(for: request) |
count += 1 |
if count < 500 { |
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) { |
self.step1() |
} |
} else { |
print("done") |
} |
} |