NSCache和背景 [英] NSCache and background

查看:97
本文介绍了NSCache和背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到当应用程序进入后台时,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?

推荐答案

在我的情况下,当存储在NSCache中的对象不符合时NSDiscardableContent协议。当我添加所述协议时,当应用程序进入后台时逐出对象消失。

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.

此外,根据NSCache.m的来源,我发现此处,不符合NSDiscardableContent协议的对象永远不会删除运行时甚至应用程序需要更多的内存,并应该逐出它的一些元素。也许这就是为什么当应用程序进入后台时非NSDiscardableContent对象被驱逐的原因,因为这是他们被驱逐的好时机。

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.

这篇关于NSCache和背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆