2

我收到一个奇怪的错误(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"
         }
    ]
}
4

1 回答 1

1

我有同样的问题。我可以通过在 Xcode 中选择我的实体然后添加 userInfo 键/值来解决它。所以对你来说,你会想要添加一个键/值'relatedByAttribute':'importKey'

查看我的答案https://stackoverflow.com/a/18370739/442695

于 2013-08-22T02:35:10.827 回答