Skip to content

Instantly share code, notes, and snippets.

@dmdeller
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmdeller/ad8a1d76bb00e3784820 to your computer and use it in GitHub Desktop.
Save dmdeller/ad8a1d76bb00e3784820 to your computer and use it in GitHub Desktop.
Import seeds into Core Data
#pragma mark - Core Data
- (void)setupCoreData
{
[MagicalRecord setupAutoMigratingCoreDataStack];
if (SearchEngine.MR_countOfEntities == 0)
{
[self importSeeds];
}
}
- (void)importSeeds
{
NSLog(@"Starting seed import");
NSDictionary *entities = NSManagedObjectContext.MR_defaultContext.persistentStoreCoordinator.managedObjectModel.entitiesByName;
for (NSString *entityName in entities.allKeys)
{
// Note: Add a 'Copy Files' build phase to the target in order to make sure this gets put in the right place
NSURL *seedURL = [NSBundle.mainBundle URLForResource:entityName withExtension:@"json" subdirectory:@"Seed Data"];
if ([NSFileManager.defaultManager fileExistsAtPath:seedURL.path])
{
NSInputStream *fileStream = [NSInputStream inputStreamWithURL:seedURL];
[fileStream open];
NSError *error = nil;
NSArray *data = [NSJSONSerialization JSONObjectWithStream:fileStream options:0 error:&error];
if (data != nil)
{
NSAssert([data isKindOfClass:NSArray.class], @"JSON must have array at root");
NSEntityDescription *entity = entities[entityName];
Class recordClass = NSClassFromString(entity.managedObjectClassName);
// Each record must have a unique primary key, and the attribute must be specified using 'relatedByAttribute' in the xcdatamodel
// https://github.com/magicalpanda/MagicalRecord/issues/180#issuecomment-6403926
[recordClass MR_importFromArray:data];
NSLog(@"Imported %@ seeds", entityName);
}
else
{
NSLog(@"Error parsing seed data for entity: %@, error: %@", entityName, error);
}
}
else
{
NSLog(@"No seed data for entity: %@", entityName);
}
}
NSLog(@"Finished seed import");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment