我收到一个奇怪的错误(NSFSagedObject(MagicalRecord)中的CFStringGetLength的EXC_BAD_ACCESS 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"
}
]
}
答案 0 :(得分:1)
我有同样的问题。我能够通过在Xcode中选择我的实体然后添加userInfo键/值来解决它。因此,对于您,您需要添加'relatedByAttribute':'importKey'
的键/值。