Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSCache and background

Tags:

ios

nscache

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?

like image 972
Luka Avatar asked Oct 31 '12 17:10

Luka


People also ask

When to use NSCache?

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.

How do I run an app in the background in Xcode?

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.

How do I keep IOS apps running in foreground?

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.

What is foreground in Iphone?

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.


1 Answers

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.

like image 87
acegs Avatar answered Sep 28 '22 01:09

acegs