简体   繁体   English

如何在ios中的NSCache中存储数据?

[英]How to store data in NSCache in ios?

I am very new to NSCache. 我是NSCache的新手。

I have an API call which results in several objects.How to store these objects in NSCache so that I don't require to call the API again. 我有一个API调用,导致几个对象。如何在NSCache中存储这些对象,以便我不需要再次调用API。

How much data can be stored in NSCache. NSCache中可以存储多少数据。 Is there a specific limit to store the data in NSCache. 是否存在将数据存储在NSCache中的特定限制。

Please help me. 请帮我。

Take a look at documentation and sample code . 看一下文档示例代码

An NSCache object is a collection-like container, or cache, that stores key-value pairs, similar to the NSDictionary class. NSCache对象是一个类似于集合的容器或缓存,用于存储键值对,类似于NSDictionary类。

Here is good explanation by Nick Zitzmann. 这里是由尼克Zitzmann很好的解释。

NSCache is similar to NSMutableDictionary, with the differences being: NSCache类似于NSMutableDictionary,区别在于:
1. It is guaranteed to be thread-safe. 1.保证是线程安全的。
2. It is much slower to access. 2.访问速度要慢得多。
3. It may throw out objects from time to time. 它可能会不时丢弃物体。 You can set costs and limits, but they're not guaranteed to be followed. 您可以设置成本和限制,但不保证可以遵循它们。
4. It is not toll-free bridged to anything in CoreFoundation. 4.在CoreFoundation中,它不是免费的桥接。
5. You can't query the number of objects that are in the cache. 5.您无法查询缓存中的对象数。
6. You can't enumerate a cache. 6.您无法枚举缓存。

I can only recommend using NSCache to store objects you wouldn't care about if they were arbitrarily destroyed. 我只能建议使用NSCache存储您不关心的对象,如果它们被任意销毁。 If the objects must not be destroyed, or if access speed is an issue, then use NSMutableDictionary instead. 如果不能销毁对象,或者访问速度是个问题,那么请改用NSMutableDictionary。

From documentation for your relevance 来自相关文档

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/NSCache_Class/Reference/Reference.html https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/NSCache_Class/Reference/Reference.html

The NSCache has a lot of auto-removal policies, which ensure that it does not use too much of the system's memory. NSCache有很多自动删除策略,可以确保它不会占用太多的系统内存。 The system automatically carries out these policies if memory is needed by other applications. 如果其他应用程序需要内存,系统会自动执行这些策略。 When invoked, these policies remove some items from the cache, minimizing its memory footprint. 调用时,这些策略会从缓存中删除一些项目,从而最大限度地减少其内存占用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM