核心数据不会为实体属性触发故障,但会为关系触发故障

时间:2016-03-01 18:57:24

标签: ios objective-c core-data

我有一个名为Trigger的核心数据实体,它与另一个名为ActiveTrigger的实体有多对一的关系。当我使用[request setReturnsObjectsAsFaults:NO];查询Trigger对象的Core Data时,查询结果在每个Trigger上有一个ActiveSetrigger实体的NSSet作为trigger.activeTriggers。但是,ActiveTrigger对象有一个数据错误,当我访问任何属性时,它不会被触发,但是当我访问ActiveTrigger上的一个关系时会触发。访问房产或关系时,故障是否应该触发?在使用关系触发错误之后,在未触发故障返回后返回(null)的相同属性是实际值。

NSFetchRequest * request = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass([Trigger class])];
[request setPredicate:[NSPredicate predicateWithFormat:@"identifier IN %@", triggerIds]];
[request setReturnsObjectsAsFaults:NO];

NSError * error = nil;
NSArray * results = [[NSManagedObjectContext sharedManagedObjectContext] executeFetchRequest:request error:&error];
for (Trigger * trigger in results) {
    for (ActiveTrigger * activeTrigger in trigger.activeTriggers) {
        NSLog(@"Here is the survey name as a property on the ActiveTrigger 1:  %@", activeTrigger.surveyName);
        NSLog(@"Here is the survey name via the relationship:                  %@", activeTrigger.survey.name);
        NSLog(@"Here is the survey name as a property on the ActiveTrigger 2:  %@", activeTrigger.surveyName);
    }
}


//Here are the logs resulting from the code above
2016-03-01 13:48:28.809 AppName[31787:1480404] Here is the survey name as a property on the ActiveTrigger 1:  (null)
2016-03-01 13:48:28.811 AppName[31787:1480404] Here is the survey name via the relationship:                  All Q
2016-03-01 13:48:28.811 AppName[31787:1480404] Here is the survey name as a property on the ActiveTrigger 2:  All Q

第一个日志之后的断点确认在尝试访问surveyName属性后,activeTrigger对象仍然存在数据错误

1 个答案:

答案 0 :(得分:0)

解决了它。事实证明,ActiveTrigger实现中的属性使用@synthesize而不是@dynamic。更改为动态,并且在访问属性时正确触发数据错误