NSCache - 标记对象不可移除 [英] NSCache - marking object not-removable

查看:177
本文介绍了NSCache - 标记对象不可移除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户通过菜单在视图控制器之间切换时,如果他有足够的内存,我想缓存它们。为了这样做,我想使用 NSCache 来存储许多 UIViewControllers

When user switches between view controllers via menu, I want to cache them if he has enough memory. In order to do so, I want to use NSCache to store many UIViewControllers.

然而,我不知道如何告诉 NSCache 当前对象当前正在使用,不应该删除。
例如,当我运行app,加载少数视图控制器并退出应用程序时, NSCache 自动删除所有 viewControllers (这是有道理的 - 应用程序将能够在前台保持更长时间)。

However, I'm not sure how I can tell NSCache that current object is currently used and should not be removed. E.g, when I run app, load few view controllers and quit the app, NSCache automatically delete all viewControllers (which makes sense - app will be able to stay longer in the foreground). However, it also removes currently displayed view controller which is not what I wanted.

如何将特定对象标记为不可移除?

How can I mark particular object as not-removable?

推荐答案

您可以这样做:

当应用程序转到背景时,缓存将被删除。
所以当应用程序进行后台存储数据从缓存到{NSUserDefaults}和应用程序didBecomeActive重新分配值到缓存。

The cache will be removed when the application goes to background. So when the application goes background store the data from cache to {NSUserDefaults} and when application didBecomeActive reassign the value to cache.

这篇关于NSCache - 标记对象不可移除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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