Integrated primaryKey extension to NSEntityDescription and refactored cache strategy

classes to eliminate issues with duplicated objects. closes #611, #612, #613, #618

* NSEntityDescription is now aware of the primaryKeyAttribute. Can be configured via
Interface Builder within Xcode or programatically.
* Added findByPrimaryKey: interface to the Core Data extensions.
* Relaxed dependencies on RKManagedObjectMapping across the system now that primaryKey is
available without a reference to the mapping.
This commit is contained in:
Blake Watters
2012-03-22 16:31:39 -04:00
parent 1b324ccc80
commit a545c3942b
32 changed files with 650 additions and 271 deletions

View File

@@ -23,6 +23,7 @@
#import "RKManagedObjectStore.h"
#import "RKDynamicObjectMappingMatcher.h"
#import "RKObjectPropertyInspector+CoreData.h"
#import "NSEntityDescription+RKAdditions.h"
#import "RKLog.h"
// Set Logging Component
@@ -62,6 +63,9 @@
self.objectClass = NSClassFromString([entity managedObjectClassName]);
_entity = [entity retain];
_objectStore = objectStore;
[self addObserver:self forKeyPath:@"entity" options:NSKeyValueObservingOptionInitial context:nil];
[self addObserver:self forKeyPath:@"primaryKeyAttribute" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:nil];
}
return self;
@@ -77,6 +81,9 @@
}
- (void)dealloc {
[self removeObserver:self forKeyPath:@"entity"];
[self removeObserver:self forKeyPath:@"primaryKeyAttribute"];
[_entity release];
[_relationshipToPrimaryKeyMappings release];
[super dealloc];
@@ -158,9 +165,7 @@
// If we have found the primary key attribute & value, try to find an existing instance to update
if (primaryKeyAttribute && primaryKeyValue) {
object = [self.objectStore.cacheStrategy findInstanceOfEntity:entity
withMapping:self
andPrimaryKeyValue:primaryKeyValue
inManagedObjectContext:[self.objectStore managedObjectContextForCurrentThread]];
withPrimaryKeyAttribute:self.primaryKeyAttribute value:primaryKeyValue inManagedObjectContext:[self.objectStore managedObjectContextForCurrentThread]];
}
if (object == nil) {
@@ -179,4 +184,19 @@
return propertyClass;
}
/*
Allows the primaryKeyAttribute property on the NSEntityDescription to configure the mapping and vice-versa
*/
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"entity"]) {
if (! self.primaryKeyAttribute) {
self.primaryKeyAttribute = [self.entity primaryKeyAttribute];
}
} else if ([keyPath isEqualToString:@"primaryKeyAttribute"]) {
if (! self.entity.primaryKeyAttribute) {
self.entity.primaryKeyAttribute = self.primaryKeyAttribute;
}
}
}
@end