NSCache with NSDiscardableContent or NSMutableDictionary?

I am currently using NSCache

to store data for my application. I can always get this data again from the server, so it's not a problem if I end up losing data, so I use NSCache

.

The problem is that every time my app enters a background NSCache

it gives out data. I don't want it to emit data unless it really is needed to improve memory, or if the object reaches its limits.

I don't want to switch to NSDictionary

and kill the app in the background if it uses too much memory. I could subclass NSDictionary

and clarify it when needed or when getting memory warnings, but I think that would be a lot of work.

I have read and found some tips for using protocol NSCache

with protocol NSDiscardableContent

, but really didn't find any example implementation code. I'm not even sure if this is the best approach to my case.

Any suggestions?

UPDATE:

I found this code on github which gives me the expected output. So it doesn't use NSDiscardableContent

, and I can't figure out what's going on, that it doesn't delete objects if the app goes into the background. I assume his method - (void)cleanUp

is called every time it NSCache

disposes of objects, and there it only arrives when the object has reached its limit or received a memory warning.

Anyway, I'm not a fan of using third party codes, so if anyone can help me emulate this behavior it will be of great help.

+3


source to share





All Articles