CoreData:错误不通过一对一关系触发

时间:2013-08-21 19:35:04

标签: core-data relationship one-to-one fault

我有三个实体:

  • Notification
  • PlanDate
  • User

关系:

  • NotificationPlanDate
  • 有一对一的关系
  • NotificationUser
  • 的关系也很多

代码:

NSLog (@"Selected Object From  Detail ViewDidLoad %@", managedObject);
NSManagedObject *planDateObject = ((PlanDate *)[managedObject valueForKey:@"plandate"]);        
NSLog(@"planDateObject %@", planDateObject);
NSString *recipientUserName = [planDateObject valueForKey:@"recipientUserName"];
NSLog(@"recipientUserName: %@", recipientUserName);

这是日志:

2013-08-21 12:26:50.349 Time[5018:c07] Selected Object From  Detail ViewDidLoad <Notification: 0xa58ee70> (entity: Notification; id: 0xb2a5480 <x-coredata://C0FB76AD-19EB-42BA-981A-F99DD6DCF6C7-5018-0000101A36CFA3D1/Notification/p0B2ABAC1-77F3-4F46-B14D-34652F148B37> ; data: {
appType = 1;
invitationType = PlanDate;
lastmoddate = "2013-08-21 17:42:42 +0000";
"notification_id" = "0B2ABAC1-77F3-4F46-B14D-34652F148B37";
plandate = "0xa1c9350 <x-coredata://C0FB76AD-19EB-42BA-981A-F99DD6DCF6C7-5018-0000101A36CFA3D1/Notification/p1C004B2B-F1DA-4EE0-9FAC-0A89E0DBCDB7>";
users = "<relationship fault: 0xb2aa210 'users'>";
})
2013-08-21 12:26:50.350 Time[5018:c07] planDateObject <Notification: 0xb292800> (entity: Notification; id: 0xa1c9350 <x-coredata://C0FB76AD-19EB-42BA-981A-F99DD6DCF6C7-5018-0000101A36CFA3D1/Notification/p1C004B2B-F1DA-4EE0-9FAC-0A89E0DBCDB7> ; data: <fault>)
2013-08-21 12:26:53.406 Time[5018:c07] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Notification 0xb292800> valueForUndefinedKey:]: the entity Notification is not key value coding-compliant for the key "recipientUserName".'

确实有一个带有Value的属性“recipientUserName”。我尝试过生成相同日志的其他属性。

为什么会出错?当我尝试访问其属性时,为什么数据显示为错误?

编辑

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

错误消息表明实体recipientUserName上实际上没有名为Notification的属性。让我们看看你的结果,看看发生了什么。首先你这样做:

NSLog (@"Selected Object From  Detail ViewDidLoad %@", managedObject);

结果如下:

2013-08-21 12:26:50.349 Time[5018:c07] Selected Object From  Detail ViewDidLoad <Notification: 0xa58ee70> (entity: Notification; id: 0xb2a5480 <x-coredata://C0FB76AD-19EB-42BA-981A-F99DD6DCF6C7-5018-0000101A36CFA3D1/Notification/p0B2ABAC1-77F3-4F46-B14D-34652F148B37> ; data: {
appType = 1;
invitationType = PlanDate;
lastmoddate = "2013-08-21 17:42:42 +0000";
"notification_id" = "0B2ABAC1-77F3-4F46-B14D-34652F148B37";
plandate = "0xa1c9350 <x-coredata://C0FB76AD-19EB-42BA-981A-F99DD6DCF6C7-5018-0000101A36CFA3D1/Notification/p1C004B2B-F1DA-4EE0-9FAC-0A89E0DBCDB7>";
users = "<relationship fault: 0xb2aa210 'users'>";
})

这表示managedObjectNotification的一个实例,而Notification的属性是:

  • appType
  • invitationType
  • lastmoddate
  • notification_id
  • plandate
  • users

plandate属性实际上是一对一的关系。根据日志消息,此关系另一端的对象是Notification的另一个实例(由x-coredata管理对象ID表示所指示)。我猜你的意思是PlanDate的一个实例,但你的日志信息说这不是你实际拥有的那个。

接下来你这样做:

NSManagedObject *planDateObject = ((PlanDate *)[managedObject valueForKey:@"plandate"]);        
NSLog(@"planDateObject %@", planDateObject);

结果是:

2013-08-21 12:26:50.350 Time[5018:c07] planDateObject <Notification: 0xb292800> (entity: Notification; id: 0xa1c9350 <x-coredata://C0FB76AD-19EB-42BA-981A-F99DD6DCF6C7-5018-0000101A36CFA3D1/Notification/p1C004B2B-F1DA-4EE0-9FAC-0A89E0DBCDB7> ; data: <fault>)

因此,如果您对planDateObject的类型有任何疑问,那么这就是它。它绝对是Notification实体的一个实例。 PlanDate的类型转换在这里没有任何意义。数据显示为<fault>的事实在这里是正常的,因为此时您还没有尝试访问其任何属性。

最后你这样做:

NSString *recipientUserName = [planDateObject valueForKey:@"recipientUserName"];

您正在尝试获取recipientUserName属性的值。但我们已经知道planDateObjectNotification,而Notification没有具有该名称的属性。因此,您尝试访问不存在的密钥时会遇到异常。

因此,如果您的PlanDate实体有一个名为recipientUserName的属性,那么您的问题是plandate关系指向错误的对象。如果PlanDate没有该属性,则会遇到无法通过您提供的信息解决的更复杂问题。