Reputation: 4572
I'm getting a strange error (EXC_BAD_ACCESS by CFStringGetLength within NSManagedObject(MagicalRecord)MR_executeFetchRequest:inContext:) I can't make sense of when attempting to import data from a JSON file...
NSManagedObjectContext *opContext = [NSManagedObjectContext MR_contextForCurrentThread];
NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"DefaultData"
ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:jsonPath];
NSError *parseError;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&parseError];
NSArray *dataTypes = [json objectForKey:@"dataTypes"];
[MagicalRecord saveWithBlockAndWait:^(NSManagedObjectContext *localContext) {
for (NSDictionary *obj in dataTypes) {
[BTDataType MR_importFromObject:obj inContext:localContext];
}
}];
NSError *saveError;
[opContext save:&saveError];
The entity is very simple, just two properties: importKey (int16) and name (String)...
The JSON is like...
{
"dataTypes":[
{
"importKey":1000,
"name":"DataType A"
},
{
"importKey":1001,
"name":"DataType B"
},
{
"importKey":1002,
"name":"DataType C"
},
{
"importKey":1003,
"name":"DataType D"
}
]
}
Upvotes: 2
Views: 771
Reputation: 11845
I had this same issue. I was able to solve it by selecting my entity in Xcode then adding a userInfo key/value. So for you, you would want to add a key/value of 'relatedByAttribute':'importKey'
.
See my answer https://stackoverflow.com/a/18370739/442695
Upvotes: 1