I have three entities:
Notification
PlanDate
User
Relationships:
Notification
has to-one relationship with PlanDate
Notification
also has to-many relationship with User
Code:
NSLog (@"Selected Object From Detail ViewDidLoad %@", managedObject);
NSManagedObject *planDateObject = ((PlanDate *)[managedObject valueForKey:@"plandate"]);
NSLog(@"planDateObject %@", planDateObject);
NSString *recipientUserName = [planDateObject valueForKey:@"recipientUserName"];
NSLog(@"recipientUserName: %@", recipientUserName);
Here is the log:
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".'
There is indeed an attribute "recipientUserName" with a Value. I have tried other attributes that produce the same log.
Why the error? Why is the Data shown as fault when I'm trying to access its attribute?
EDIT
The error message says that there is not actually an attribute named recipientUserName
on the entity Notification
. Let's run through your results to see what happened. First you do this:
NSLog (@"Selected Object From Detail ViewDidLoad %@", managedObject);
And the result is this:
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'>";
})
This says that managedObject
is an instance of Notification
, and that Notification
's attributes are:
appType
invitationType
lastmoddate
notification_id
plandate
users
The plandate
attribute is actually a to-one relationship. According to the log message, the object at the other end of this relationship is another instance of Notification
(as indicated by the x-coredata
managed object ID representation). I'm guessing you meant this to be an instance of PlanDate
, but your log message says that this isn't what you actually have there.
Next you do this:
NSManagedObject *planDateObject = ((PlanDate *)[managedObject valueForKey:@"plandate"]);
NSLog(@"planDateObject %@", planDateObject);
And the result is:
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>)
So if you had any doubt about what type planDateObject
is, this nails it. It's most definitely an instance of the Notification
entity. The typecast to PlanDate
doesn't mean anything here. The fact that the data is shown as <fault>
is normal here, because at this point you haven't tried to access any of its attributes.
Finally you do this:
NSString *recipientUserName = [planDateObject valueForKey:@"recipientUserName"];
You're trying to get the value of the recipientUserName
attribute. But we already know that planDateObject
is a Notification
, and that Notification
does not have an attribute with that name. So, you get an exception from trying to access a nonexistent key.
So, if your PlanDate
entity has an attribute named recipientUserName
, then your problem is that the plandate
relationship is pointing at the wrong object. If PlanDate
does not have that attribute, you have more complex problems that can't be solved from the information you've provided.