// // <%= config[:class_prefix] %>AppDelegate+Frank.h // <%= config[:project_name] %> // // Created by <%= config[:project_creator] %> on 06/02/2013. // Copyright (c) 2013 <%= config[:organization_name] %>. All rights reserved. // #import "<%= config[:class_prefix] %>AppDelegate+Frank.h" #import #import #import @implementation <%= config[:class_prefix] %>AppDelegate (Frank) - (void) seedCoreData:(NSString *)json { NSDictionary *objectsDictionary = [json objectFromJSONString]; NSManagedObjectContext *managedObjectContext = [RKObjectManager sharedManager].objectStore.managedObjectContextForCurrentThread; for (NSString *objectKey in objectsDictionary) { [self createObjectWithEntityName:objectKey configuredWithDictionary:[objectsDictionary objectForKey:objectKey] inManagedObjectContext:managedObjectContext]; } NSError *error = nil; if (![managedObjectContext save:&error]) { NSLog(@"Error saving: %@", [error localizedDescription]); } } - (NSManagedObject *)createObjectWithEntityName:(NSString *)entityName configuredWithDictionary:(NSDictionary *)dictionary inManagedObjectContext:(NSManagedObjectContext *)context { NSManagedObject *object = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:context]; NSEntityDescription *objectEntityDescription = [object entity]; NSDictionary *attributes = [objectEntityDescription attributesByName]; NSDictionary *relationships = [objectEntityDescription relationshipsByName]; for (NSString *key in [dictionary allKeys]) { if ([[attributes allKeys] containsObject:key]) { id value = [dictionary objectForKey:key]; if (value != [NSNull null]) { [object setValue:value forKey:key]; } } else if ([[relationships allKeys] containsObject:key]) { NSRelationshipDescription *relationshipDescription = [relationships objectForKey:key]; NSString *destinationEntityName = [[relationshipDescription destinationEntity] name]; if ([relationshipDescription isToMany]) { NSMutableSet *relationshipSet = [NSMutableSet set]; for (NSDictionary *toManyRelationshipObject in [dictionary objectForKey:key]) { NSManagedObject *destinationObject = [self createObjectWithEntityName:destinationEntityName configuredWithDictionary:toManyRelationshipObject inManagedObjectContext:context]; [relationshipSet addObject:destinationObject]; } [object setValue:relationshipSet forKey:key]; } else { NSManagedObject *destinationObject = [self createObjectWithEntityName:destinationEntityName configuredWithDictionary:[dictionary objectForKey:key] inManagedObjectContext:context]; [object setValue:destinationObject forKey:key]; } } } return object; } @end