Inherits from NSObject
Conforms to NSLocking
Declared in CC3Cache.h

Overview

Instances of CC3Cache hold cachable objects, which are stored and retrieved by name.

Objects must implement the CC3Cacheable protocol to be stored in the cache. Objects in the cache must have a unique name. An assertion error will be raised if an object with the same name exists in the cache already. To replace an object in the cache, you must first remove the existing object from the cache.

CC3Cache implements the NSLocking protocol, and all access to the cache contents is thread-safe.

Each object may be held either strongly or weakly by this cache, depending on the value of the isWeak property at the time the object was added to the cache.

Properties

isWeak

Indicates whether this cache holds weak references to the objects within.

@property (nonatomic, assign) BOOL isWeak

Discussion

If the value of this property is YES, this cache will hold a weak reference to each object within the cache, and the presence of the object in the cache will not stop the object from being deallocated. In this case, each object should automatically remove itself from this cache during deallocation, once all external strong references to the object have been released.

This property is set during cache instantiation and initialization, and can be changed at any time, allowing the cache to hold a mix of weakly and strongly cached objects. Whether an object is weakly or strongly cached is determined at the time the object is added to the cached.

Declared In

CC3Cache.h

typeName

A descriptive name of the type of object being cached.

@property (nonatomic, retain, readonly) NSString *typeName

Declared In

CC3Cache.h

Class Methods

strongCacheForType:

Allocates and initializes an empty autoreleased instance holding strong references to the objects within the cache. The presence of an object in this cache will stop the object from being deallocated.

+ (id)strongCacheForType:(NSString *)typeName

Declared In

CC3Cache.h

weakCacheForType:

Allocates and initializes an empty autoreleased instance holding weak references to the objects within the cache. The presence of an object in this cache will not stop the object from being deallocated.

+ (id)weakCacheForType:(NSString *)typeName

Declared In

CC3Cache.h

Instance Methods

addObject:

Adds the specified object to the cache.

- (void)addObject:(id<CC3Cacheable>)obj

Discussion

Objects in the cache must have a unique name. An assertion error will be raised if an object with the same name exists in the cache already. To replace an object in the cache, you must first remove the existing object from the cache.

If the value of the isWeak property is YES at the time this method is invoked, this cache will hold a weak reference to the specified object. In this case, the specified object should automatically remove itself from this cache during deallocation, once all external strong references to it have been released, to avoid bad accesses to the cached object after it has been deallocated.

If the value of the isWeak property is NO at the time this method is invoked, this cache will hold a strong reference to the specified object, and it cannot be deallocated until it is specifically removed from this cache.

Declared In

CC3Cache.h

enumerateObjectsUsingBlock:

Enumerates all of the objects in the cache with the specified block.

- (void)enumerateObjectsUsingBlock:(void ( ^ ) ( id<CC3Cacheable> obj , BOOL *stop ))block

Discussion

The block takes as arguments an object from the cache, and a pointer to a boolean that can be set to YES to stop enumeration.

Declared In

CC3Cache.h

getObjectNamed:

Returns the cached object with the specified name, or nil if an object with that name has not been cached.

- (id<CC3Cacheable>)getObjectNamed:(NSString *)name

Declared In

CC3Cache.h

initAsWeakCache:forType:

Initializes this instance as either a weak or strong cache, for holding objects of the specified content type.

- (id)initAsWeakCache:(BOOL)isWeak forType:(NSString *)typeName

Discussion

The specified type name is used for logging and reporting informational purposes only. It should be a lower-case descriptive name, such as “resource”, “texture”, “shader”, etc.

If the value of the specified flag is YES, this cache will hold weak references to the objects within the cache, and the presence of the object in the cache will not stop the object from being deallocated. If the value of the specified flag is NO, this cache will hold strong references to the objects within the cache, and the presence of the object in the cache will stop the object from being deallocated.

Declared In

CC3Cache.h

objectsSortedByName

Returns an array of all objects in this cache, sorted by name.

- (NSArray *)objectsSortedByName

Discussion

This is a convenience method for use when listing items in the array during logging and development.

Since the objects are retained within the returned array, be careful not to hold on to the array, if you want weakly cached objects to be automatically removed from this cache.

Declared In

CC3Cache.h

removeAllObjects

Removes all objects from the cache.

- (void)removeAllObjects

Discussion

This is typically invoked when 3D capabilities are no longer required, or will not be needed for a significant time, and the app wishes to release 3D resources. This method checks each cache entry, and logs an info message for any object that is weakly cached, as the existence of a weakly-cached entry is an indication that the cached object is being retained somewhere else within the app, and is therefore a potential source of a memory leak.

Declared In

CC3Cache.h

removeAllObjectsOfType:

Removes all objects that are instances of the specified class, or a subclass.

- (void)removeAllObjectsOfType:(Class)type

Discussion

This is typically invoked when strongly cached objects are no longer required, or will not be needed for a significant time, and the app wishes to release 3D resources. This method checks each cache entry, and logs an info message for any object that is weakly cached, as the existence of a weakly-cached entry is an indication that the cached object is being retained somewhere else within the app, and is therefore a potential source of a memory leak.

Declared In

CC3Cache.h

removeObject:

Removes the specified object from the cache.

- (void)removeObject:(id<CC3Cacheable>)obj

Declared In

CC3Cache.h

removeObjectNamed:

Removes the object with the specified name from the cache.

- (void)removeObjectNamed:(NSString *)name

Declared In

CC3Cache.h