Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 6, 2012, 08:59 PM   #1
daproject85
macrumors member
 
Join Date: Apr 2011
NSCache VS NSPurgableData

Hi Forum,

I have read the apple docs for both NScache and NSPurgableData and still dont know the pros and cons. Any help is appreciated..

and also the totalcostlimit and countlimit methods of the NSCache class...... thetotalcost sets the size in megabytes? kilobytes? gigabytes?

the countlimit clearly handles the # of items, but what about if i want a cache with a 10MB limit
daproject85 is offline   0 Reply With Quote
Old Nov 7, 2012, 08:35 AM   #2
Reason077
macrumors 65816
 
Join Date: Aug 2007
If you wanted an NSCache with a 10MB limit, you'd:

- set totalCostLimit to (1024 * 1024).
- Use setObject:forKey:cost: when adding objects to the cache, specifying the size of each object you're adding (in bytes).

The NSCache should then start automatically evicting objects once the size exceeds 10MB.
Reason077 is offline   0 Reply With Quote
Old Nov 7, 2012, 01:05 PM   #3
daproject85
Thread Starter
macrumors member
 
Join Date: Apr 2011
Quote:
Originally Posted by Reason077 View Post
If you wanted an NSCache with a 10MB limit, you'd:

- set totalCostLimit to (1024 * 1024).
- Use setObject:forKey:cost: when adding objects to the cache, specifying the size of each object you're adding (in bytes).

The NSCache should then start automatically evicting objects once the size exceeds 10MB.
Reasons: so the unit for totalcostlimit is in bytes then?
daproject85 is offline   0 Reply With Quote
Old Nov 7, 2012, 02:03 PM   #4
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by daproject85 View Post
Reasons: so the unit for totalcostlimit is in bytes then?
The cost units are arbitrary. The units are whatever you want them to be. You add items with a cost given for each one. As long as they're all using the same units, everything will work.

Suppose you want a limit 100 MB, with units of 1 MB. Then the limit would be the integer 100, and each item added has an associated integer cost measured in MB: 1, 2, 3, etc..

If you want the same limit of 100 MB, with units of 100 KB, then multiply everything by 10. So the limit would be 1000, and every item's integer cost would be measured in tenths of a MB.
chown33 is offline   0 Reply With Quote
Old Nov 8, 2012, 03:53 AM   #5
Reason077
macrumors 65816
 
Join Date: Aug 2007
Quote:
Originally Posted by daproject85 View Post
Reasons: so the unit for totalcostlimit is in bytes then?
As chown33 says, the 'cost' can be whatever unit you want. eg: Kilowatts, Yuan Renminbi, or even magic beans. But I would strongly recommend using bytes in most instances.

Using megabytes as your unit, as chown33 suggests, might not be a good idea as it would be subject to fairly wide variance due to rounding. What would happen if you added 1 million objects of 1kb each, for example?
Reason077 is offline   0 Reply With Quote
Old Nov 8, 2012, 11:19 AM   #6
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by Reason077 View Post
Using megabytes as your unit, as chown33 suggests, might not be a good idea as it would be subject to fairly wide variance due to rounding. What would happen if you added 1 million objects of 1kb each, for example?
I wasn't suggesting megabytes as units, merely using it as an illustrative example.

The main thing that would be a problem is giving an item a cost of zero.

There should never be a zero-cost object. The mathematical reasons for this should be obvious. You can add 0 to any number, any number of times, and the resulting sum will be unchanged. So 900 + 0 = 900 + 0 + 0 + 0 + 0 + 0 + 0 + 0. Clearly, giving items a cost of 0 causes them to "disappear" from all calculations involving the limit.

Maybe there's failsafe code in NSCache that imposes a lower limit on cost, so passing 0 is actually evaluated as a cost of 1 (the minimum usable cost). Or maybe it throws an exception if a cost is 0. Personally, I wouldn't bet on anything; simply avoid zero or add your own failsafe.
chown33 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Forum Jump

All times are GMT -5. The time now is 11:31 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC