gpt4 book ai didi

ios - 应用程序进入后台时清空 NSCache

转载 作者:可可西里 更新时间:2023-11-01 03:55:45 33 4
gpt4 key购买 nike

我目前使用 NSCache 的子类来存储一些图像(值)及其相应的名称(键),当应用程序位于前台时,它似乎工作正常。但是,当我按下主页按钮/用户进入后台并重新进入应用程序时,NSCache 是空的。

实现细节:我将 NSCache 的子类实现为单例。这确保该类应该只有一个实例,只需调用类名和共享实现,就可以从程序中的任何地方访问该实例。 [HelloCache 共享缓存]。这里的HelloCache是​​子类的名字,sharedCache是​​共享的Cache。

最佳答案

认为您需要确保放置在缓存中的对象符合 NSDiscardableContentProtocol得到想要的结果。来自NSCache Class Reference :

A common data type stored in NSCache objects is an object that implements the NSDiscardableContent protocol. Storing this type of object in a cache has benefits, because its content can be discarded when it is not needed anymore, thus saving memory. By default, NSDiscardableContent objects in the cache are automatically removed from the cache if their content is discarded, although this automatic removal policy can be changed. If an NSDiscardableContent object is put into the cache, the cache calls discardContentIfPossible on it upon its removal.

关于ios - 应用程序进入后台时清空 NSCache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33811996/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com