Note that there are some explanatory texts on larger screens.

plurals
  1. POpopulating core data model through JSON with multiple Entities
    text
    copied!<p>Having an issue populating a core data model with two entities through a JSON file,</p> <p>I managed to populate the first entity with no issues, but once i tried populating the second entity, received an error stating the data parameter is nil,</p> <p>Ive checked that I've used the correct json file name (Weights.json).</p> <p>Below is the code:</p> <pre><code> // Create the managed object context NSManagedObjectContext *context = managedObjectContext(); // Save the managed object context NSError *error = nil; if (![context save:&amp;error]) { NSLog(@"Error while saving %@", ([error localizedDescription] != nil) ? [error localizedDescription] : @"Unknown Error"); exit(1); } NSError* err = nil; NSString* dataPath = [[NSBundle mainBundle] pathForResource:@"Exercises" ofType:@"json"]; NSArray* exercisesFromJSON = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath] options:kNilOptions error:&amp;err]; //NSLog(@"Imported exercisesFromJSON: %@", exercisesFromJSON); [exercisesFromJSON enumerateObjectsUsingBlock:^(NSDictionary *exerciseDictionary, NSUInteger idx, BOOL *stop) { Exercise *exercise = [NSEntityDescription insertNewObjectForEntityForName:@"Exercise" inManagedObjectContext:context]; NSDictionary *attributes = [[exercise entity] attributesByName]; for (NSString *attribute in [attributes allKeys]) { id value = [exerciseDictionary objectForKey:attribute]; if (value == nil) { continue; } [exercise setValue:value forKey:attribute]; } NSError *error; if (![context save:&amp;error]) { NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]); } }]; NSString* secondDataPath = [[NSBundle mainBundle] pathForResource:@"Weights" ofType:@"json"]; NSArray* weightsFromJSON = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:secondDataPath] options:kNilOptions error:&amp;err]; ///***Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'****////// NSLog(@"Imported weightsFromJSON: %@", weightsFromJSON); [weightsFromJSON enumerateObjectsUsingBlock:^(NSDictionary *weightDictionary, NSUInteger idx, BOOL *stop) { Weight *weight = [NSEntityDescription insertNewObjectForEntityForName:@"Weight" inManagedObjectContext:context]; NSDictionary *attributes = [[weight entity] attributesByName]; for (NSString *attribute in [attributes allKeys]) { id value = [weightDictionary objectForKey:attribute]; if (value == nil) { continue; } [weight setValue:value forKey:attribute]; } NSError *error; if (![context save:&amp;error]) { NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]); } }]; </code></pre> <p>thank you in advance for any help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload