Permalink
Browse files

Abstract away the use of an NSHashTable

This will soon be changing to improve iOS 5/Lion compatibility.
  • Loading branch information...
1 parent 46d481f commit 80b8e31e71f0a775d8a0ac1e9ca6d0266a80e5a2 @brentdax committed Oct 28, 2012
Showing with 68 additions and 8 deletions.
  1. +1 −1 ANSession+ANResource_Private.h
  2. +8 −7 ANSession.m
  3. +21 −0 _ANIdentifiedResourceSet.h
  4. +38 −0 _ANIdentifiedResourceSet.m
@@ -13,7 +13,7 @@
@interface ANSession ()
-- (id)uniqueResource:(ANResource*)resource;
+- (id)uniqueResource:(ANIdentifiedResource*)resource;
@end
View
@@ -8,6 +8,7 @@
#import "ANSession.h"
#import "ANSession+ANResource_Private.h"
+#import "_ANIdentifiedResourceSet.h"
const ANResourceID ANMeUserID = 0;
const ANResourceID ANUnspecifiedPostID = 0;
@@ -16,7 +17,7 @@
@interface ANSession ()
-@property (strong,nonatomic) NSHashTable * resources;
+@property (strong,nonatomic) _ANIdentifiedResourceSet * resourceSet;
@property (strong,nonatomic) dispatch_queue_t resourceUniquingQueue;
@end
@@ -44,7 +45,7 @@ + (BOOL)isUsingNetwork {
- (id)init {
if((self = [super init])) {
- _resources = [NSHashTable weakObjectsHashTable];
+ _resourceSet = [_ANIdentifiedResourceSet new];
_resourceUniquingQueue = dispatch_queue_create("ANSession resource uniquing queue", DISPATCH_QUEUE_SERIAL);
}
return self;
@@ -171,20 +172,20 @@ - (void)completeFilterListRequest:(ANFilterListRequestCompletion)completion with
}
}
-- (id)uniqueResource:(ANResource *)r {
- __block ANResource * resource = r;
+- (id)uniqueResource:(ANIdentifiedResource *)r {
+ __block ANIdentifiedResource * resource = r;
// We do this in a queue so that only one thread can be adjusting the resource set at a time.
dispatch_sync(self.resourceUniquingQueue, ^{
- ANResource * existingResource = [self.resources member:resource];
+ ANIdentifiedResource * existingResource = [self.resourceSet existingResource:resource];
if(existingResource) {
- [self.resources removeObject:existingResource];
+ [self.resourceSet removeResource:existingResource];
existingResource.representation = resource.representation;
resource = existingResource;
}
- [self.resources addObject:resource];
+ [self.resourceSet addResource:resource];
});
return resource;
View
@@ -0,0 +1,21 @@
+//
+// _ANIdentifiedResourceSet.h
+// Alef
+//
+// Created by Brent Royal-Gordon on 10/27/12.
+// Copyright (c) 2012 Architechies. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "ANIdentifiedResource.h"
+
+// This table is used by ANSession to unique identified resources without holding strong references to them.
+// No user serviceable parts inside.
+
+@interface _ANIdentifiedResourceSet : NSObject
+
+- (void)addResource:(ANIdentifiedResource*)resource;
+- (ANIdentifiedResource*)existingResource:(ANIdentifiedResource*)resource;
+- (void)removeResource:(ANIdentifiedResource*)resource;
+
+@end
View
@@ -0,0 +1,38 @@
+//
+// _ANIdentifiedResourceSet.m
+// Alef
+//
+// Created by Brent Royal-Gordon on 10/27/12.
+// Copyright (c) 2012 Architechies. All rights reserved.
+//
+
+#import "_ANIdentifiedResourceSet.h"
+
+@interface _ANIdentifiedResourceSet ()
+
+@property (strong,nonatomic) NSHashTable * resources;
+
+@end
+
+@implementation _ANIdentifiedResourceSet
+
+- (id)init {
+ if((self = [super init])) {
+ _resources = [NSHashTable weakObjectsHashTable];
+ }
+ return self;
+}
+
+- (void)addResource:(ANIdentifiedResource *)resource {
+ [self.resources addObject:resource];
+}
+
+- (ANIdentifiedResource *)existingResource:(ANIdentifiedResource *)resource {
+ return [self.resources member:resource];
+}
+
+- (void)removeResource:(ANIdentifiedResource *)resource {
+ [self.resources removeObject:resource];
+}
+
+@end

0 comments on commit 80b8e31

Please sign in to comment.