我收到一个奇怪的错误(EXC_BAD_ACCESS by CFStringGetLength 在 NSManagedObject(MagicalRecord)MR_executeFetchRequest:inContext:) 尝试从 JSON 文件导入数据时我无法理解......
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];
实体很简单,只有两个属性:importKey(int16)和name(String)...
JSON就像...
{
"dataTypes":[
{
"importKey":1000,
"name":"DataType A"
},
{
"importKey":1001,
"name":"DataType B"
},
{
"importKey":1002,
"name":"DataType C"
},
{
"importKey":1003,
"name":"DataType D"
}
]
}