Clean tests, added cached predicate usage for fetch request cache strategy. refs #739

This commit is contained in:
Blake Watters
2012-05-12 19:18:29 -04:00
parent 715ae6cbf7
commit 15f41c6622
14 changed files with 126 additions and 742 deletions

View File

@@ -20,7 +20,8 @@
- (id)initWithManagedObjectContext:(NSManagedObjectContext *)context
{
self = [self init];
NSAssert(context, @"Cannot initialize entity cache with a nil context");
self = [super init];
if (self) {
_managedObjectContext = [context retain];
_attributeCaches = [[NSMutableSet alloc] init];
@@ -29,6 +30,11 @@
return self;
}
- (id)init
{
return [self initWithManagedObjectContext:nil];
}
- (void)dealloc
{
[_managedObjectContext release];
@@ -38,6 +44,8 @@
- (void)cacheObjectsForEntity:(NSEntityDescription *)entity byAttribute:(NSString *)attributeName
{
NSAssert(entity, @"Cannot cache objects for a nil entity");
NSAssert(attributeName, @"Cannot cache objects without an attribute");
RKEntityByAttributeCache *attributeCache = [self attributeCacheForEntity:entity attribute:attributeName];
if (attributeCache && !attributeCache.isLoaded) {
[attributeCache load];
@@ -51,12 +59,16 @@
- (BOOL)isEntity:(NSEntityDescription *)entity cachedByAttribute:(NSString *)attributeName
{
NSAssert(entity, @"Cannot check cache status for a nil entity");
NSAssert(attributeName, @"Cannot check cache status for a nil attribute");
RKEntityByAttributeCache *attributeCache = [self attributeCacheForEntity:entity attribute:attributeName];
return (attributeCache && attributeCache.isLoaded);
}
- (NSManagedObject *)objectForEntity:(NSEntityDescription *)entity withAttribute:(NSString *)attributeName value:(id)attributeValue
{
NSAssert(entity, @"Cannot retrieve cached objects with a nil entity");
NSAssert(attributeName, @"Cannot retrieve cached objects by a nil entity");
RKEntityByAttributeCache *attributeCache = [self attributeCacheForEntity:entity attribute:attributeName];
if (attributeCache) {
return [attributeCache objectWithAttributeValue:attributeValue];
@@ -67,6 +79,8 @@
- (NSSet *)objectsForEntity:(NSEntityDescription *)entity withAttribute:(NSString *)attributeName value:(id)attributeValue
{
NSAssert(entity, @"Cannot retrieve cached objects with a nil entity");
NSAssert(attributeName, @"Cannot retrieve cached objects by a nil entity");
RKEntityByAttributeCache *attributeCache = [self attributeCacheForEntity:entity attribute:attributeName];
if (attributeCache) {
return [attributeCache objectsWithAttributeValue:attributeValue];
@@ -77,6 +91,8 @@
- (RKEntityByAttributeCache *)attributeCacheForEntity:(NSEntityDescription *)entity attribute:(NSString *)attributeName
{
NSAssert(entity, @"Cannot retrieve attribute cache for a nil entity");
NSAssert(attributeName, @"Cannot retrieve attribute cache for a nil attribute");
for (RKEntityByAttributeCache *cache in self.attributeCaches) {
if ([cache.entity isEqual:entity] && [cache.attribute isEqualToString:attributeName]) {
return cache;
@@ -88,6 +104,7 @@
- (NSSet *)attributeCachesForEntity:(NSEntityDescription *)entity
{
NSAssert(entity, @"Cannot retrieve attribute caches for a nil entity");
NSMutableSet *set = [NSMutableSet set];
for (RKEntityByAttributeCache *cache in self.attributeCaches) {
if ([cache.entity isEqual:entity]) {
@@ -105,6 +122,7 @@
- (void)addObject:(NSManagedObject *)object
{
NSAssert(object, @"Cannot add a nil object to the cache");
NSSet *attributeCaches = [self attributeCachesForEntity:object.entity];
for (RKEntityByAttributeCache *cache in attributeCaches) {
[cache addObject:object];
@@ -113,6 +131,7 @@
- (void)removeObject:(NSManagedObject *)object
{
NSAssert(object, @"Cannot remove a nil object from the cache");
NSSet *attributeCaches = [self attributeCachesForEntity:object.entity];
for (RKEntityByAttributeCache *cache in attributeCaches) {
[cache removeObject:object];