Note that there are some explanatory texts on larger screens.

plurals
  1. POAttribute of relationship in Core Data is returning nil when being called in iOS
    text
    copied!<p>I have an iOS application where I am using Core Data for storage. I have two entities ("MyEntity", and "OtherEntity") which are in a one to one relationship. Both entities are related to each other (i.e. each entity has an inverse relationship with the other). In addition to having a relationship with each other, one of the attributes of each entity is also the primarykey of the other entity. The problem I am having is that I realize that I shouldn't have an attribute that is the foreign key of another entity, when the entities have a relationship with each other, however I am unable to retrieve the primary key indirectly by referencing the relationship attribute, but I am able to retrieve it by referencing the attribute that I explicitly set to the primary key of the other entity:</p> <pre><code>//NSInteger userId = [testUser.otherEntity.userId integerValue]; --&gt; returns nil NSInteger userId = [testUser.userId integerValue]; --&gt; works fine </code></pre> <p>where "testUser" is an instance of type "MyEntity" which is a subclass of NSManagedObject, and "otherEntity" is an instance of the Entity, "OtherEntity" that has an inverse relationship with "MyEntity".</p> <p>Here are the attributes of each Entity:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import &lt;CoreData/CoreData.h&gt; @class OtherEntity; @interface MyEntity : NSManagedObject @property (nonatomic, strong) NSString * email; @property (nonatomic, strong) NSNumber * primaryId; //primary key for MyEntity @property (nonatomic, strong) NSString * metaData; @property (nonatomic, strong) NSDate * birthDate; @property (nonatomic, strong) NSString * name; @property (nonatomic, strong) NSNumber * userId; //this is the primary key for OtherEntity @property (nonatomic, strong) OtherEntity *otherEntity; @end </code></pre> <p>and here are the attributes for OtherEntity:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import &lt;CoreData/CoreData.h&gt; @class MyEntity; @interface OtherEntity : NSManagedObject @property (nonatomic, strong) NSString * address; @property (nonatomic, strong) NSString * city; @property (nonatomic, strong) NSString * country; @property (nonatomic, strong) NSString * fax; @property (nonatomic, strong) NSString * phone; @property (nonatomic, strong) NSString * postalCode; @property (nonatomic, strong) NSString * province; @property (nonatomic, strong) NSNumber * myUserId//primary key of MyEntity @property (nonatomic, strong) NSNumber * userId;//primary key of Other Entity @property (nonatomic, strong) MyEntity *myEntity; @end </code></pre> <p>Can anyone explain to me what the problem is, or what it is that I'm doing wrong?</p> <p>Thanks in advance to all who reply.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload