G. Shearer
G. Shearer

Reputation: 2185

CoreData - One side of relationship is always nil

I have a CoreData project where there are two related entities (Users and Studies), these entities have a one-to-many relationship to another entity (UserStudy) which has a one-to-one relationship to each of the first two entities, and some additional attributes.

I am experiencing a very strange issue. I can add a UserStudy to the collections on Users and Studies, but the other side of the relation never works. Every time I set it, I get no errors, and the relationship is simply null.

  1. I have already created a test project using the identical code, it works there for some reason, but not in my year old project (large codebase I cannot simply trash)
  2. I have already cleaned
  3. I have already deleted derived data
  4. I have tried setting the relationship using the dot accessor, the convenience setter, and using setValue:ForKey.
  5. I have verified that all inverse relationships are properly set.
  6. I have verified that the relationship is one-to-many on the User and Study sides, and one-to-one on the UserStudy side.

The code where I try to set the relationship looks like:

CRMUser *aUser = [NSEntityDescription insertNewObjectForEntityForName:@"CRMUser"
                                               inManagedObjectContext:self.ctx];
CRMStudy *aStudy = [NSEntityDescription insertNewObjectForEntityForName:@"CRMStudy"
                                                 inManagedObjectContext:self.ctx];
CRMUserStudy *aUserStudy = [NSEntityDescription insertNewObjectForEntityForName:@"CRMUserStudy"
                                                         inManagedObjectContext:self.ctx];
aUserStudy.study = aStudy;
aUserStudy.user = aUser;

STAssertTrue([aUserStudy.user isEqual:aUser], @"newUserStudyWithUser:study:token failed to set user");
STAssertTrue([aUserStudy.study isEqual:aStudy], @"newUserStudyWithUser:study:token failed to set study");

NSLog(@"aUserStudy: %@",aUserStudy);

Which always fails, and gives me this log:

aUserStudy: <CRMUserStudy: 0xa6db8e0> (entity: CRMUserStudy; id: 0xa6dc580 <x-coredata:///CRMUserStudy/t1BDB3E8A-F5F0-4462-AB88-C5186AC4D8C671> ; data: {
    study = nil;
    token = nil;
    user = nil;
})

But if I do:

CRMUser *aUser = [NSEntityDescription insertNewObjectForEntityForName:@"CRMUser"
                                               inManagedObjectContext:self.ctx];
CRMStudy *aStudy = [NSEntityDescription insertNewObjectForEntityForName:@"CRMStudy"
                                                 inManagedObjectContext:self.ctx];
CRMUserStudy *aUserStudy = [NSEntityDescription insertNewObjectForEntityForName:@"CRMUserStudy"
                                                         inManagedObjectContext:self.ctx];
[aUser addUserStudyObject:aUserStudy];
[aStudy addUserStudyObject:aUserStudy];

The relationship is set, but still nil on the other side.

Like I said, I can copy-paste my model code into a new project and everything works fine. Has anyone ever experienced this? Maybe it has something to do with the test target?

Upvotes: 0

Views: 751

Answers (1)

G. Shearer
G. Shearer

Reputation: 2185

I still don't know what the specific cause of this problem was, I am assuming that my .xcdatamodeld file had somehow become corrupted.

I had already tried deleting the UserStudy entity, and recreating it, with no luck.

Then I created a new model version.
I DID NOT set this new version as the current default. This did not fix the problem.

Then I once again deleted and recreated the UserStudy entity.
Suddenly everything started working.

So I am assuming that creating the new model version caused XCode to regenerate my model files, and fix whatever corruption was causing the problem. Note, I ran a diff on the old model file vs the new one, and nothing appears to be different, so I'm at a loss of what the actual issue was besides that the model must have been corrupted somehow.

Upvotes: 1

Related Questions