I am using NSCache
for caching objects. Is there a way to delete all cache at once?
I am using the following methods to cache:
class VC: UIViewController {
static let userCache = NSCache<NSString, User>()
func getUser(fromId id: String, completion: @escaping (_ user: User) -> Void) {
if let cachedVersion = VC.userCache.object(forKey: id as NSString) {
print("using cached user from id")
completion(cachedVersion)
return
}
// retrieve new instance of object
}
}
But I see the Documents & Data section increasing by time (currently 140MB+). I tried to clear cache using the following method, but that didn't work out:
VC.userCache.removeAllObjects()
Is there a way to retrieve an old instance of NSCache
so I do not have to create a new instance each time the app is loaded? Or is there a way to delete all cached objects when the app loads?