Skip to content
This repository has been archived by the owner on Sep 12, 2018. It is now read-only.

Fix dictionary mapping #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Classes/NSManagedObject+Mappings.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,11 @@
*/
+ (NSString *)primaryKey;

/**
Remote key for primaryKey. E.g., @c @@"remote_id".

@return A remote key name or primaryKey if not overriden
*/
+ (NSString *)remotePrimaryKey;

@end
17 changes: 15 additions & 2 deletions Classes/NSManagedObject+Mappings.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,17 @@ + (id)objectOrSetOfObjectsFromValue:(id)value ofClass:class inContext:(NSManaged
if ([value isKindOfClass:class])
return value;

if ([value isKindOfClass:[NSDictionary class]])
return [class findOrCreate:value inContext:context];
if ([value isKindOfClass:[NSDictionary class]]) {
NSString *primaryValue = value[ [class remotePrimaryKey] ];

if (primaryValue) {
NSManagedObject *object = [class findOrCreate:@{ [class primaryKey]: primaryValue } inContext:context];
[object update:value];
return object;
}

return [class findOrCreate:value inContext:context];
}

if ([value isKindOfClass:[NSArray class]])
return [NSSet setWithArray:[value map:^id(id object) {
Expand Down Expand Up @@ -116,4 +125,8 @@ + (id)primaryKey {
userInfo:nil];
}

+ (NSString *)remotePrimaryKey {
return [self primaryKey];
}

@end