Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// GCReusableQueue.h | ||
// GCReusableQueue | ||
// | ||
// Created by Glenn Chiu on 31-05-12. | ||
// Copyright (c) 2012 Dot Square. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
/* Make sure that objects conform to this protocol and return -reuseIdentifier. */ | ||
@protocol ReusableObject <NSObject> | ||
|
||
- (NSString *)reuseIdentifier; | ||
|
||
@end | ||
|
||
@interface GCReusableQueue : NSObject | ||
|
||
- (void)enqueueReusableObject:(id <ReusableObject>)obj; | ||
- (id <ReusableObject>)dequeueReusableObjectWithIdentifier:(NSString *)identifier; | ||
|
||
/* This method should not be used, as the queue will discard objects automatically | ||
when memory gets tight. Use this method to discard objects manually. */ | ||
- (void)clearQueue; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// | ||
// GCReusableQueue.m | ||
// GCReusableQueue | ||
// | ||
// Created by Glenn Chiu on 31-05-12. | ||
// Copyright (c) 2012 Dot Square. All rights reserved. | ||
// | ||
|
||
#import "GCReusableQueue.h" | ||
#import <TargetConditionals.h> | ||
|
||
@implementation GCReusableQueue | ||
{ | ||
NSCache *_reusableObjects; | ||
id _observer; | ||
} | ||
|
||
- (id)init | ||
{ | ||
self = [super init]; | ||
if (self) | ||
{ | ||
|
||
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR | ||
|
||
__weak GCReusableQueue *w_self = self; | ||
|
||
self->_observer = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidReceiveMemoryWarningNotification | ||
object:nil | ||
queue:nil | ||
usingBlock:^(NSNotification *note) { | ||
GCReusableQueue *s_self = w_self; | ||
|
||
[s_self clearQueue]; | ||
}]; | ||
|
||
#endif | ||
|
||
} | ||
return self; | ||
} | ||
|
||
- (void)dealloc | ||
{ | ||
[[NSNotificationCenter defaultCenter] removeObserver:self->_observer]; | ||
} | ||
|
||
- (NSCache *)reusableObjects | ||
{ | ||
if (!self->_reusableObjects) | ||
{ | ||
self->_reusableObjects = [NSCache new]; | ||
} | ||
|
||
return self->_reusableObjects; | ||
} | ||
|
||
- (id <ReusableObject>)dequeueReusableObjectWithIdentifier:(NSString *)identifier | ||
{ | ||
NSParameterAssert(identifier); | ||
|
||
NSMutableSet *objects = [[self reusableObjects] objectForKey:identifier]; | ||
|
||
id <ReusableObject> obj = [objects anyObject]; | ||
|
||
if (obj) | ||
{ | ||
[objects removeObject:obj]; | ||
} | ||
|
||
return obj; | ||
} | ||
|
||
- (void)enqueueReusableObject:(id <ReusableObject>)obj | ||
{ | ||
NSMutableSet *objects = [[self reusableObjects] objectForKey:[obj reuseIdentifier]]; | ||
|
||
if (!objects) | ||
{ | ||
objects = [NSMutableSet set]; | ||
[[self reusableObjects] setObject:objects forKey:[obj reuseIdentifier]]; | ||
} | ||
else | ||
{ | ||
[objects addObject:obj]; | ||
} | ||
} | ||
|
||
- (void)clearQueue | ||
{ | ||
[[self reusableObjects] removeAllObjects]; | ||
} | ||
|
||
@end |