0

I have a Person entity in Core Data model. I want to create a "friends" relationship conceptually which essentially is a self-referencing relationship (ie. expect friends fetch to return a list of "Person" objects. However, there is NO inverse relationship set.

I get the following exception:

app[7713:90b] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Person 0xd55dd70> valueForUndefinedKey:]: the entity Person is not key value coding-compliant for the key "vprofiles".'

Question is, why does this cause an error, let alone an application crash? As an object graph, shouldn't this work out-of-the-box given its a common mapping pattern?

The App crashes on this line:

NSArray *friends = [[person valueForKey:@"vprofiles"] allObjects];

My Person interface generated is below.

@interface Person : NSManagedObject

@property (nonatomic, retain) NSDate * createdAt;
@property (nonatomic, retain) NSString * email;
@property (nonatomic, retain) NSDate * updatedAt;
@property (nonatomic, retain) NSString * username;
@property (nonatomic, retain) NSSet *vprofiles;
@end

@interface Person (CoreDataGeneratedAccessors)

- (void)addVprofilesObject:(Person *)value;
- (void)removeVprofilesObject:(Person *)value;
- (void)addVprofiles:(NSSet *)values;
- (void)removeVprofiles:(NSSet *)values;

@end
appledevguru
  • 105
  • 9
  • That should work if the relationship is defined correctly. Perhaps an upper/lowercase problem? Did you generate NSManagedObject subclass files Person.h/Person.m? If not, please do so (from the Xcode Edit menu. Then show us the contents of Person.h. – Martin R Apr 19 '14 at 20:50
  • And there should be no reason *not* to define an inverse relationship. If all friendships are mutual then `friends` can be the inverse relationship of itself. – Martin R Apr 19 '14 at 20:52
  • @MartinR Thanks Martin, I updated my posting to show the generated object. The strange thing is, this was working before and I didn't have any issues when on my device I viewed the tableView that references the "friends" relationship (ie. I didn't see any app crashes). Also, I never made any changes since my last migration do my model, so I am confused why this came up out of nowhere. :-( – appledevguru Apr 19 '14 at 21:15
  • I cannot tell you why it worked before, but there is no "friends" relationship in the Person class, so it cannot work. – Martin R Apr 19 '14 at 21:16
  • Also, I am using SCM, so I reverted my code to the previous working copy that i know for 100% sure didn't show this exception when I visited the View that "fetches" the friends relationship. Is this an external problem...perhaps the simulator or something foreign all together? I know I didn't change the model and there is no logic aside from default placeholder code to use this relationship, but app crashes nevertheless. – appledevguru Apr 19 '14 at 21:18
  • @MartinR Yes sorry, I think you missed the update to the description where I mentioned that "friends" was just a conceptual term I am using, but internally I am defining it as "vprofiles". I updated the code for fetching the relationship to be consistent. :-) – appledevguru Apr 19 '14 at 21:21
  • Yes, I missed that. (It is often confusing if not the actual code and error messages are posted.) - So your error message is about missing KVC compliance for "vprofiles"? Did you add the "vprofiles" relationship in a new model version and forgot to set the "current model version" to the new version? Otherwise I have no idea why it should fail. – Martin R Apr 19 '14 at 21:35
  • @MartinR I agree with you on my mishap. It won't happen again. I am still fairly new to this service, but it is very much invaluable, so I will take greater care on my future postings. I have updated my posting to address the clarity issue. Yeah, it's still complaining about KVC-compliance. I'll keep hacking around and see if anything works. :-) – appledevguru Apr 19 '14 at 21:46
  • You could NSLog the value of `person.entity.properties` to see the actual properties of your object. – Martin R Apr 19 '14 at 21:51

0 Answers0