Skip to content

Instantly share code, notes, and snippets.

@zorn
Created March 31, 2010 16:03
Show Gist options
  • Save zorn/350500 to your computer and use it in GitHub Desktop.
Save zorn/350500 to your computer and use it in GitHub Desktop.
// Core Data question...
// So I have a unit test like this:
- (void)testNewClientFromBusiness
{
PTBusiness *business = [modelController newBusiness];
STAssertTrue([[business clients] count] == 0, @"is actually %d", [[business clients] count]);
PTClient *client = [business newClient];
STAssertTrue([business isEqual:[client business]], nil);
STAssertTrue([[business clients] count] == 1, @"is actually %d", [[business clients] count]);
}
// When I implement -newClient inside of PTBusiness like this:
- (PTClient *)newClient
{
PTClient *client = [NSEntityDescription insertNewObjectForEntityForName:@"Client" inManagedObjectContext:[self managedObjectContext]];
[client setBusiness:self];
[client updateLocalDefaultsBasedOnBusiness];
return client;
}
// The test fails cause [[business clients] count] is still 0 after -newClient is called.
// If I impliment it like this:
- (PTClient *)newClient
{
PTClient *client = [NSEntityDescription insertNewObjectForEntityForName:@"Client" inManagedObjectContext:[self managedObjectContext]];
NSMutableSet *group = [self mutableSetValueForKey:@"clients"];
[group addObject:client];
[client updateLocalDefaultsBasedOnBusiness];
return client;
}
// The tests passes.
// So am I right in thinking the inverse relationship is only updated when I interact with the mutable set?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment