Reputation: 1359
Can anyone help figure out what i've done wrong within this code? trying to populate core data structure through JSON file
// Create the managed object context
NSManagedObjectContext *context = managedObjectContext();
// Save the managed object context
NSError *error = nil;
if (![context save:&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* Exercises = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath]
options:kNilOptions
error:&err];
//NSLog(@"Imported Exercises: %@", Exercises);
NSManagedObject *object = [NSEntityDescription insertNewObjectForEntityForName:@"Exercise" inManagedObjectContext:context];
NSString *theJSONString = @"{\"key\":\"value\"}";
NSError *theError = NULL;
NSDictionary *jsonDict = [NSDictionary dictionaryWithJSONString:theJSONString error:&theError];
Exercise *exercise = [NSEntityDescription insertNewObjectForEntityForName:@"Exercise"
inManagedObjectContext:context];
[Exercises enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSDictionary *attributes = [[object entity] attributesByName];
for (NSString *attribute in attributes) {
id value = [jsonDict objectForKey:attribute];
if (value == nil) {
continue;
}
[exercise setValue:value forKey:attribute];
}
NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
}];
Edited: Code now compiles but i get null values in the core data model. Thanks
Upvotes: 0
Views: 248
Reputation: 70976
First, this isn't a SQLite error, it's a standard, basic Objective-C error. You'd have the same problem even if you weren't using a SQLite-based Core Data store.
Let's work backward from where it looks like the error is occurring, which is:
NSDictionary *attributes = [[obj entity] attributesByName];
You're calling entity
on obj
. But where did obj
come from? From here:
[Exercises enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
OK, so obj
came from Exercises
. What is in Exercises
?
NSArray* Exercises = [NSJSONSerialization
JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath]
options:kNilOptions
error:&err];
Exercises
is an array of things that NSJSONSerialization
gave you. The JSONObjectWithData:options:error:
method gives you Cocoa equivalents for the JSON content. It does not give you managed objects. In this case it looks like your JSON produced an array of NSDictionary
.
So what happened is:
NSJSONSerialization
and got an array of dictionariesentity
on the members of the array, only the array members are NSDictionary
instances, and that class does not implement entity
.Your code never creates any Core Data objects, anywhere. That's why you're not getting any Core Data objects. Your code is crashing because you're trying to call Core Data methods on objects that don't implement those methods.
Upvotes: 1