Permalink
Browse files

Integrate optimization for utilizing the managed object cache when co…

…nnecting relationships. closes #693

Thanks to Ed McManus (emcmanus) for the implementation.
  • Loading branch information...
blakewatters committed May 16, 2012
1 parent 2ef10ee commit 3fb2b023fa0f5e4ecbf5840f28135c3db3746cb0
Showing with 15 additions and 10 deletions.
  1. +15 −10 Code/CoreData/RKManagedObjectMappingOperation.m
@@ -22,6 +22,7 @@
#import "RKManagedObjectMapping.h"
#import "NSManagedObject+ActiveRecord.h"
#import "RKDynamicObjectMappingMatcher.h"
+#import "RKManagedObjectCaching.h"
#import "RKManagedObjectStore.h"
#import "RKLog.h"
@@ -68,18 +69,22 @@ - (void)connectRelationship:(NSString *)relationshipName {
id relatedObject = nil;
if ([valueOfLocalPrimaryKeyAttribute conformsToProtocol:@protocol(NSFastEnumeration)]) {
RKLogTrace(@"Connecting has-many relationship at keyPath '%@' to object with primaryKey attribute '%@'", relationshipName, primaryKeyAttributeOfRelatedObject);
- NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K IN %@", primaryKeyAttributeOfRelatedObject, valueOfLocalPrimaryKeyAttribute];
- NSFetchRequest *fetchRequest = [NSManagedObject requestAllInContext:[self.destinationObject managedObjectContext]];
- fetchRequest.predicate = predicate;
- fetchRequest.entity = objectMapping.entity;
- NSArray *objects = [NSManagedObject executeFetchRequest:fetchRequest inContext:[self.destinationObject managedObjectContext]];
- relatedObject = [NSSet setWithArray:objects];
+
+ // Implemented for issue 284 - https://github.com/RestKit/RestKit/issues/284
+ relatedObject = [NSMutableSet set];
+ NSObject<RKManagedObjectCaching> *cache = [[(RKManagedObjectMapping*)[self objectMapping] objectStore] cacheStrategy];
+ for (id foreignKey in valueOfLocalPrimaryKeyAttribute) {
+ id searchResult = [cache findInstanceOfEntity:objectMapping.entity withPrimaryKeyAttribute:primaryKeyAttributeOfRelatedObject value:foreignKey inManagedObjectContext:[[(RKManagedObjectMapping*)[self objectMapping] objectStore] managedObjectContextForCurrentThread]];
+ if (searchResult) {
+ [relatedObject addObject:searchResult];
+ }
+ }
} else {
RKLogTrace(@"Connecting has-one relationship at keyPath '%@' to object with primaryKey attribute '%@'", relationshipName, primaryKeyAttributeOfRelatedObject);
- NSFetchRequest *fetchRequest = [NSManagedObject requestFirstByAttribute:primaryKeyAttributeOfRelatedObject withValue:valueOfLocalPrimaryKeyAttribute inContext:[self.destinationObject managedObjectContext]];
- fetchRequest.entity = objectMapping.entity;
- NSArray *objects = [NSManagedObject executeFetchRequest:fetchRequest inContext:[self.destinationObject managedObjectContext]];
- relatedObject = [objects lastObject];
+
+ // Normal foreign key
+ NSObject<RKManagedObjectCaching> *cache = [[(RKManagedObjectMapping*)[self objectMapping] objectStore] cacheStrategy];
+ relatedObject = [cache findInstanceOfEntity:objectMapping.entity withPrimaryKeyAttribute:primaryKeyAttributeOfRelatedObject value:valueOfLocalPrimaryKeyAttribute inManagedObjectContext:[[(RKManagedObjectMapping*)[self objectMapping] objectStore] managedObjectContextForCurrentThread]];
}
if (relatedObject) {
RKLogDebug(@"Connected relationship '%@' to object with primary key value '%@': %@", relationshipName, valueOfLocalPrimaryKeyAttribute, relatedObject);

0 comments on commit 3fb2b02

Please sign in to comment.