mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-05-12 03:06:30 +08:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user