0

What I'm try to do?

Remove existing responses in URLCache prior a given date.

How I try to do that?

removeCachedResponses(since: date)

 func resetCache(_ since: Date) {
        self.cache.removeCachedResponses(since: since)
    }

Cashed responses where stored with:

...
let cachedData = CachedURLResponse(response: response, data: data)
self.cache.storeCachedResponse(cachedData, for: request)
...

Deleting cached responses works with

self.cache.removeAllCachedResponses()

EDIT: Only when you wait a bit. Imho the removing is done async. For that reason not every image is deleted immediately from cache. So I decided to add a small wait before start loading fresh one.

func reloadImages() {
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            self.loadImages(for: self.dataModel.loadDemoData(count: 5))
        }
        
    }

and

self.cache.removeCachedResponse(for: request)

But the cashed request aren't removed Do I have to use specify a specific date format or do I have to save a date value during storing the cache?

  • You are not using passing argument. You just deleting data that has been created 1min ago till now. Instead of `cache.removeCachedResponses(since: resetDate)` try `cache.removeCachedResponses(since: since)` – matsotaa Aug 08 '22 at 18:43
  • @matsotaa omg i totally misunderstood this: "The earliest date of responses that should remain in the cache. Any responses with dates later than this parameter should be removed." – rotecodefraktion Aug 08 '22 at 19:02
  • @matsotaa I changed the code so it deletes with day - 1, that works sometime and sometime not. – rotecodefraktion Aug 10 '22 at 19:41
  • I can assume first time worked, but second not? If so I can assume it correct properly since after the first time the are nothing to delete? If I missed something I need more info, cause for me it seems it should work as expected – matsotaa Aug 15 '22 at 14:53

0 Answers0