From 2351fc2472d01feb27587b58b47cfb2e00c75505 Mon Sep 17 00:00:00 2001 From: Blake Watters Date: Mon, 21 Jan 2013 13:18:26 -0500 Subject: [PATCH] Add unit test and fix for crash during eviction of cache keys during query for compound identification attributes. closes #1171 --- Code/CoreData/RKEntityByAttributeCache.m | 11 +++++---- .../CoreData/RKEntityByAttributeCacheTest.m | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/Code/CoreData/RKEntityByAttributeCache.m b/Code/CoreData/RKEntityByAttributeCache.m index 324c6427..39924865 100644 --- a/Code/CoreData/RKEntityByAttributeCache.m +++ b/Code/CoreData/RKEntityByAttributeCache.m @@ -226,7 +226,6 @@ static NSArray *RKCacheKeysForEntityFromAttributeValues(NSEntityDescription *ent - (NSSet *)objectsWithAttributeValues:(NSDictionary *)attributeValues inContext:(NSManagedObjectContext *)context { - // TODO: Assert that the attribute values contains all of the cache attributes!!! NSMutableSet *objects = [NSMutableSet set]; NSArray *cacheKeys = RKCacheKeysForEntityFromAttributeValues(self.entity, attributeValues); for (NSString *cacheKey in cacheKeys) { @@ -281,10 +280,12 @@ static NSArray *RKCacheKeysForEntityFromAttributeValues(NSEntityDescription *ent { @synchronized(self.cacheKeysToObjectIDs) { if (attributeValues && [attributeValues count]) { - NSString *cacheKey = RKCacheKeyForEntityWithAttributeValues(self.entity, attributeValues); - NSMutableArray *objectIDs = [self.cacheKeysToObjectIDs objectForKey:cacheKey]; - if (objectIDs && [objectIDs containsObject:objectID]) { - [objectIDs removeObject:objectID]; + NSArray *cacheKeys = RKCacheKeysForEntityFromAttributeValues(self.entity, attributeValues); + for (NSString *cacheKey in cacheKeys) { + NSMutableArray *objectIDs = [self.cacheKeysToObjectIDs objectForKey:cacheKey]; + if (objectIDs && [objectIDs containsObject:objectID]) { + [objectIDs removeObject:objectID]; + } } } else { RKLogWarning(@"Unable to remove object for object ID %@: empty values dictionary for attributes '%@'", objectID, self.attributes); diff --git a/Tests/Logic/CoreData/RKEntityByAttributeCacheTest.m b/Tests/Logic/CoreData/RKEntityByAttributeCacheTest.m index 909079af..1ae253aa 100644 --- a/Tests/Logic/CoreData/RKEntityByAttributeCacheTest.m +++ b/Tests/Logic/CoreData/RKEntityByAttributeCacheTest.m @@ -465,4 +465,28 @@ // trying to lookup with empty dictionary // using weird attribute types as cache keys +- (void)testEvictionOfArrayOfIdentifierAttributes +{ + // Put some objects into the cache + // Delete them + RKHuman *human1 = [NSEntityDescription insertNewObjectForEntityForName:@"Human" inManagedObjectContext:self.managedObjectContext]; + human1.railsID = [NSNumber numberWithInteger:12345]; + RKHuman *human2 = [NSEntityDescription insertNewObjectForEntityForName:@"Human" inManagedObjectContext:self.managedObjectContext]; + human2.railsID = [NSNumber numberWithInteger:56789]; + [self.managedObjectContext save:nil]; + + [self.cache addObject:human1]; + [self.cache addObject:human2]; + + self.cache.monitorsContextForChanges = NO; + + NSDictionary *attributeValues = @{ @"railsID" : @[ human1.railsID, human2.railsID ] }; + + [self.managedObjectContext deleteObject:human1]; + [self.managedObjectContext deleteObject:human2]; + [self.managedObjectContext save:nil]; + + [self.cache objectsWithAttributeValues:attributeValues inContext:self.managedObjectContext]; +} + @end