NSCache and background

前端 未结 3 1395
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-14 20:52

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 exp

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-14 21:19

    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.

提交回复
热议问题