How to store data in NSCache in ios?
Asked Answered
F

2

7

I am very new to 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.

How much data can be stored in NSCache. Is there a specific limit to store the data in NSCache.

Please help me.

Foveola answered 10/10, 2012 at 10:43 Comment(0)
I
11

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.

Here is good explanation by Nick Zitzmann.

NSCache is similar to NSMutableDictionary, with the differences being:
1. It is guaranteed to be thread-safe.
2. It is much slower to access.
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.
5. You can't query the number of objects that are in the cache.
6. You can't enumerate a cache.

I can only recommend using NSCache to store objects you wouldn't care about if they were arbitrarily destroyed. If the objects must not be destroyed, or if access speed is an issue, then use NSMutableDictionary instead.

Inhospitality answered 10/10, 2012 at 10:49 Comment(1)
Wondering, how much slower is it?Musgrove
K
1

From documentation for your relevance

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. 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.

Karolekarolina answered 10/10, 2012 at 10:55 Comment(3)
Thanks a lot for your explanation..But how much data can be stored in NSCache?Can we store 2MB of data in NSCache?Foveola
@Pavi Take a look at documentation. setTotalCostLimit: and setCountLimit: method.Inhospitality
@ParagBafna Thanks.Let me check that.Foveola

© 2022 - 2024 — McMap. All rights reserved.