-
-
Save beccadax/3473115 to your computer and use it in GitHub Desktop.
// self.resources is an NSMutableSet of ANResource objects. | |
// I want to remove the objects that have no other references in the app. | |
// (This is intended to be called in response to an iOS memory warning.) | |
// Will this do the trick? | |
- (void)discardUnusedResources { | |
// We pass the objects through a weak reference; if they're not | |
// referenced anywhere else in the app, they won't survive the journey. | |
NSMutableSet * newResources = [NSMutableSet new]; | |
NSUInteger oldResourceCount = self.resources.count; | |
__weak ANResource * weakResource; | |
while((weakResource = [self.resources anyObject])) { | |
[self.resources removeObject:weakResource]; | |
__strong ANResource * strongResource = weakResource; | |
if(strongResource) { | |
[newResources addObject:strongResource]; | |
} | |
} | |
self.resources = newResources; | |
NSLog(@"-[ANSession discardUnusedResources]: started with %u resources, ended with %u", oldResourceCount, self.resources.count); | |
} |
@belkadan, NSCache drops the entire cache on the floor under memory pressure. That's not what I want—it would lose references to objects that are still active. The main purpose of this set is not caching, but uniquing—ensuring that each of the external resources is represented by exactly one ANResource object. (Think of the way a Core Data managed object context always has exactly one object for each record—that's what I'm doing.) If I used NSCache, then a cache drop could cause a new object to be created for a resource which already has an old object, and the old object would slowly become staler and staler.
I'm using an NSHashTable for now, but I'm not really happy with that solution, because I want this code to run on iOS 5 as well as 6...
No one here's heard of NSCache?