Add findInstancesOfEntity method to cache to support one to many relationships.

This commit is contained in:
Charlie Savage
2012-05-30 15:24:40 -06:00
committed by Blake Watters
parent f0e8bbe284
commit f368f9a16c
3 changed files with 45 additions and 4 deletions

View File

@@ -56,6 +56,22 @@ static NSString * const RKInMemoryObjectManagedObjectCacheThreadDictionaryKey =
return [entityCache objectForEntity:entity withAttribute:primaryKeyAttribute value:primaryKeyValue];
}
- (NSArray *)findInstancesOfEntity:(NSEntityDescription *)entity
withPrimaryKeyAttribute:(NSString *)primaryKeyAttribute
value:(id)primaryKeyValue
inManagedObjectContext:(NSManagedObjectContext *)managedObjectContext
{
RKEntityCache *entityCache = [self cacheForEntity:entity inManagedObjectContext:managedObjectContext];
if (! [entityCache isEntity:entity cachedByAttribute:primaryKeyAttribute]) {
RKLogInfo(@"Caching instances of Entity '%@' by primary key attribute '%@'", entity.name, primaryKeyAttribute);
[entityCache cacheObjectsForEntity:entity byAttribute:primaryKeyAttribute];
RKEntityByAttributeCache *attributeCache = [entityCache attributeCacheForEntity:entity attribute:primaryKeyAttribute];
RKLogTrace(@"Cached %ld objects", (long) [attributeCache count]);
}
return [entityCache objectsForEntity:entity withAttribute:primaryKeyAttribute value:primaryKeyValue];
}
- (void)didFetchObject:(NSManagedObject *)object
{
RKEntityCache *entityCache = [self cacheForEntity:object.entity inManagedObjectContext:object.managedObjectContext];