Currently, I'm using NSCache
to store data for my app. I can always retrieve that data again from the server, so it is not a problem if I eventually lose the data, that's why I'm using NSCache
.
The problem is that every time my app enters background NSCache
evicts data. I don't want it to evict data unless it is really necessary for memory improvement or if the object reaches its limits.
I don't want to switch to NSDictionary
and get my app killed on the background if it uses too much memory. I could subclass NSDictionary
and make it clear itself when needed or when getting memory warnings, but I guess this would be a lot of work.
I read around and found a few suggestions to use NSCache
with NSDiscardableContent
protocol, but didn't really found any sample code of implementation. I'm not even sure if this would be the best approach to my case.
Any suggestions?
UPDATE:
I found this code in github which gives me the expected result. Therefore, it doesn't use the NSDiscardableContent
and I cannot identify what is going on that it doesn't remove objects in case app enters background state. I guess his method - (void)cleanUp
is called every time NSCache
will remove objects and there he only proceeds if object has reached its limit or received memory warning.
Anyway, I'm not a fan of using third party codes, so if anyone can help me mimic this behavior would be a great help.