Ios NSFetchRequest-从关系中提取实体

Ios NSFetchRequest-从关系中提取实体,ios,core-data,nspredicate,nsfetchrequest,Ios,Core Data,Nspredicate,Nsfetchrequest,我有两个实体:报警和预约,它们有一对一的关系 现在,我想使用NSFetchRequest检索给定约会的警报 我试过: NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:ALARM_ENTITY_NAME]; fetchRequest.predicate = [NSPredicate predicateWithFormat:@"forAppointment.objectID = %@", [appoi

我有两个实体:报警和预约,它们有一对一的关系

现在,我想使用
NSFetchRequest
检索给定约会的警报

我试过:

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:ALARM_ENTITY_NAME];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"forAppointment.objectID = %@", [appointment objectID]];
它抛出:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“实体中找不到appoition.objectID的密钥路径”

有人知道我如何做到这一点吗?

只需使用

[NSPredicate predicateWithFormat:@"forAppointment = %@", appointment]

只有当
约会
来自不同的托管对象时,才需要使用
对象ID
上下文那么

[NSPredicate predicateWithFormat:@"forAppointment = %@", [appointment objectID]]
应该有用。(
==%@
的右侧可以是托管对象或其对象ID。
您不必在谓词本身中指定“objectID”)

如果两者之间存在1-1关系(并且如果您已经为实体实现了
NSManagedObject
子类),则无需获取。只需走关键路径:

Alarm *alarm = appointment.alarm;
或者,如果您使用的是primitive
NSManagedObject

NSManagedObject *alarm = [appointment valueForKeyPath:@"alarm"];