Skip to content

Commit

Permalink
made safe import public so you can directly import into an object if …
Browse files Browse the repository at this point in the history
…you know whats going on!
  • Loading branch information
tonymillion committed Jan 6, 2013
1 parent d08788d commit 0a9eaaa
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
18 changes: 13 additions & 5 deletions TMCoreData/NSManagedObject/NSManagedObject+TMCDExporting.m
Expand Up @@ -132,17 +132,25 @@ -(NSDictionary*)exportToDictionaryWithSet:(NSMutableSet*)objectSet includeRelati
else
{
// its 1:1
TMCDLog(@"1:1 relationship found!");
TMCDLog(@"1:1 relationship found!: %@", description);
NSEntityDescription* destination = [description destinationEntity];
Class class = NSClassFromString([destination managedObjectClassName]);
DLog(@"destination: %@", destination);

finalvalue = [class exportToDictionaryWithSet:objectSet includeRelationships:includeRelationships];
// Class class = NSClassFromString([destination managedObjectClassName]);

id relatedObject = [self valueForKey:relationship];

finalvalue = [relatedObject exportToDictionaryWithSet:objectSet
includeRelationships:includeRelationships];

}
}

[mutableDict setObject:finalvalue
forKey:relationship];
if(finalvalue)
{
[mutableDict setObject:finalvalue
forKey:relationship];
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions TMCoreData/NSManagedObject/NSManagedObject+TMCDImporting.h
Expand Up @@ -10,6 +10,8 @@

@interface NSManagedObject (TMCDImporting)

-(void)safeImportValuesFromDictionary:(NSDictionary*)dict;

+(id)importFromDictionary:(NSDictionary*)dict inContext:(NSManagedObjectContext*)context;
+(NSArray*)importFromArray:(NSArray*)dictArray inContext:(NSManagedObjectContext*)context;

Expand Down
40 changes: 34 additions & 6 deletions TMCoreData/NSManagedObject/NSManagedObject+TMCDImporting.m
Expand Up @@ -155,8 +155,7 @@ -(void)safeImportValuesFromDictionary:(NSDictionary*)dict
id value = [dict objectForKey:relationship];
if(!value)
continue;



if([self importValue:value
forKey:relationship])
{
Expand Down Expand Up @@ -184,16 +183,18 @@ +(id)importFromDictionary:(NSDictionary*)dict inContext:(NSManagedObjectContext*
{
id exisingObject = nil;

if([self respondsToSelector:@selector(findExistingWithDictionary:)])
if([self respondsToSelector:@selector(findExistingWithDictionary:inContext:)])
{
SEL findExistingSEL = @selector(findExistingWithDictionary:);
SEL findExistingSEL = @selector(findExistingWithDictionary:inContext:);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
exisingObject = [[self class] performSelector:findExistingSEL
withObject:dict];
withObject:dict
withObject:context];

#pragma clang diagnostic pop
}

// ok lets check the "primaryKey" KVP on the entity user info
if(!exisingObject)
{
Expand Down Expand Up @@ -249,6 +250,33 @@ +(NSArray*)importFromArray:(NSArray*)dictArray inContext:(NSManagedObjectContext

+(id)objectWithObject:(id)arrayOrDictionary inContext:(NSManagedObjectContext*)context
{
if([arrayOrDictionary isKindOfClass:[NSString class]] || [arrayOrDictionary isKindOfClass:[NSNumber class]])
{
// this is probably a single primary key
// this probably needs an entity type eh ?

id stringOrNumber = arrayOrDictionary;

NSString *primaryKeyName = [[self entityDescriptionInContext:context] primaryKey];

if(primaryKeyName)
{
if(stringOrNumber)
{
NSManagedObject *managedObject = [self findFirstWhereProperty:primaryKeyName
isEqualTo:stringOrNumber
inContext:context];

if(managedObject)
{
TMCDLog(@"Existing Object Found by primaryKey");
return managedObject;
}
}
}
return nil;
}

if([arrayOrDictionary isKindOfClass:[NSDictionary class]])
{
return [self importFromDictionary:arrayOrDictionary
Expand Down

0 comments on commit 0a9eaaa

Please sign in to comment.