I've noticed that NSCache evicts all of its object when the application goes in background. is that the expected behaviour? is there a way to avoid it?
I would expect it to evict objects when the device run out of memory not immediately when the app goes in background.
Do you know any valid alternative?
You typically use NSCache objects to temporarily store objects with transient data that are expensive to create. Reusing these objects can provide performance benefits, because their values do not have to be recalculated. However, the objects are not critical to the application and can be discarded if memory is tight.
In Xcode in the top click your Apps name then click Edit Scheme... Then click Run on the left, then click the Options tab, half way down it will say Background Fetch, check that. Then when you run your app it will start in the background.
If Background refresh is greyed out in the ON position, go To Settings App - > General - > Background App Refresh - > Turn on the option for the system, and then you can turn on / off by app.
Overview. Use foreground transitions to prepare your app's UI to appear onscreen. An app's transition to the foreground is usually in response to a user action. For example, when the user taps the app's icon, the system launches the app and brings it to the foreground.
In my case, that happened when objects stored in NSCache does not conform to NSDiscardableContent protocol. When I added the said protocol, eviction of objects when the app is entering background disappears.
In addition, based on source of NSCache.m I found here, objects that do not conform to NSDiscardableContent protocol are never removed at runtime even the app needs more memory and should evict some of its elements. Maybe that's the reason why non-NSDiscardableContent objects are evicted when the app is entering background because that's a good time for them to be evicted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With