From 604c84473026a7a136be0b47a9f635a3f51f5965 Mon Sep 17 00:00:00 2001 From: Evadne Wu Date: Fri, 1 Jun 2012 17:02:33 +0800 Subject: [PATCH 1/2] adds readme --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..350207e --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +# IRObjectQueue + +Simple object re-queueing like `UITableView`. + +## Usage + + IRObjectQueue *objectQueue = [IRObjectQueue new]; + [objectQueue addObject:anObject]; // must conform to and return -reuseIdentifier + + // Later + [objectQueue dequeueObjectWithIdentifier:anIdentifier]; + + // When memory runs low + [objectQueue removeAllObjects]; + +## Authors + +* [Evadne Wu](http://radi.ws) at [Iridia Productions](http://iridia.tw) & [Waveface Inc.](http://waveface.com). \ No newline at end of file From a6f886ac342f2629f8b991b480c4779136a4373e Mon Sep 17 00:00:00 2001 From: Evadne Wu Date: Tue, 5 Jun 2012 18:03:28 +0800 Subject: [PATCH 2/2] makes the object queue automatically purge all the items when memory runs low --- IRObjectQueue/IRObjectQueue.h | 2 ++ IRObjectQueue/IRObjectQueue.m | 37 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/IRObjectQueue/IRObjectQueue.h b/IRObjectQueue/IRObjectQueue.h index fcd7f56..a7a1deb 100644 --- a/IRObjectQueue/IRObjectQueue.h +++ b/IRObjectQueue/IRObjectQueue.h @@ -21,4 +21,6 @@ - (void) removeAllObjects; +@property (nonatomic, readwrite, assign) BOOL purgesAutomatically; // Default YES + @end diff --git a/IRObjectQueue/IRObjectQueue.m b/IRObjectQueue/IRObjectQueue.m index f6e8079..2e9323e 100644 --- a/IRObjectQueue/IRObjectQueue.m +++ b/IRObjectQueue/IRObjectQueue.m @@ -7,6 +7,11 @@ // #import "IRObjectQueue.h" +#import + +#if TARGET_OS_IPHONE +#import +#endif @interface IRObjectQueue () @@ -18,6 +23,31 @@ - (NSMutableSet *) mutableSetForIdentifier:(NSString *)identifier; @implementation IRObjectQueue @synthesize identifierToObjects = _identifierToObjects; +@synthesize purgesAutomatically = _purgesAutomatically; + +- (id) init { + + self = [super init]; + if (!self) + return nil; + + _purgesAutomatically = YES; + +#if TARGET_OS_IPHONE + + __weak IRObjectQueue *wSelf = self; + + [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidReceiveMemoryWarningNotification object:nil queue:nil usingBlock:^(NSNotification *note) { + + [wSelf handleMemoryWarning:note]; + + }]; + +#endif + + return self; + +} - (NSMutableDictionary *) identifierToObjects { @@ -70,4 +100,11 @@ - (NSMutableSet *) mutableSetForIdentifier:(NSString *)identifier { } +- (void) handleMemoryWarning:(NSNotification *)note { + + if (self.purgesAutomatically) + [self removeAllObjects]; + +} + @end \ No newline at end of file