NSCache and background
Asked Answered
O

3

15

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?

Osteotome answered 31/10, 2012 at 17:25 Comment(0)
Q
13

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.

Quid answered 27/11, 2012 at 8:20 Comment(0)
M
4

NSCache continues to be ridiculous. I've been an iOS dev since day 1 of the SDK and only just today I got bit by this and found this helpful post.

For all these years, I thought it was working like any normal cache should, but nope! I'll never use it again.

Megdal answered 20/12, 2018 at 20:40 Comment(0)
C
1

The comments in this related post indicate that an NSCache is cleared when the app enters the background. NSCache is not evicting data

Convertite answered 31/10, 2012 at 17:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.