CoreData - Adding a new relationship to existing entity
I have the following basic problem:
I have two entities, person and department.
Before adding a new person I want to check, that the department does not already exists and if so, then link the new person to the existing department.
Simple insert with a new department relationship:
Department *department = (Department *)[NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:self.context];
department.groupName = @"Office of Personnel Management";
Person *person1 = (Person *)[NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.context];
person1.name = @"John Smith";
per开发者_JAVA百科son1.birthday = [self dateFromString:@"12-1-1901"];
person1.department = department;
Person *person2 = (Person *)[NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.context];
person2.name = @"Jane Doe";
person2.birthday = [self dateFromString:@"4-13-1922"];
person2.department = department;
department.manager = person1;
department.members = [NSSet setWithObjects:person1, person2, nil];
the last line makes the linkage - that's ok.
But what if I want to do the following, after the execution of the code above:
[self checkForExistingDepartment:@"Office of Personnel Management"];
if(self.existingDepartment) {
// here is my Problem number 1:
// department = ???
(NSEntityDescription *) department = self.existingDepartment;
} else {
Department *department = (Department *)[NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:self.context];
department.groupName = @"Office of Personnel Management";
}
Person *person1 = (Person *)[NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.context];
person1.name = @"John Smith the second one";
person1.birthday = [self dateFromString:@"12-1-1901"];
person1.department = department;
// former line for adding new: department.members = [NSSet setWithObjects:person1, nil];
// and here is my problem number 2:
// I think I need something like: [NSSet addObjects:person1, nil];
In short form my problem are duplicated entries in table department.
Perhaps someone knows a good CoreData tutorial which is good for beginner with advanced SQL knowledge. (Searching on google or reading the developer documentation for hours is not that helpful as I thought :) )
For me as a beginner it's important to now whether I'm on the right way or not, can anybody confirm this?
Thanks and greetings,
matthias
You're defining the department variable inside the if/else
statement and then using a different one outside of it. Try changing your if
to look like :
[self checkForExistingDepartment:@"Office of Personnel Management"];
if(self.existingDepartment) {
department = self.existingDepartment;
} else {
department = (Department *)[NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:self.context];
department.groupName = @"Office of Personnel Management";
}
Though without seeing the code for checkForExistingDepartment we can't help you very much . . .
Sorry, the check function looks like this:
- (void) checkForExistingDepartment:(NSString *)searchDepartment {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Department" initManagedObjectContext:self.context];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"country" ascending:YES selector:nil];
NSArray *descriptors = [NSArray arrayWithObject:sortDescriptor];
[fetchRequest setSortDescriptor:descriptors];
NSString *query = searchDepartment;
if(query && query.length) {
fetchRequest.predicate = [NSPredicate predicatWithFormat:@"country contains[cd] %@", query];
}
NSError *error;
self.fetchedResultsController = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:self.context
sectionNameKeyPath:@"section"
cacheName:@"Root"];
self.fetchedResultsController.delegate = self;
[self.fetchedResultsController release];
if(![[self fetchedResultsController] performFetch:&error]) {
NSLog(@"Error %@", [error localizedDescription]);
}
self.existingDepartment = [self.fetchedResultsController objectAtIndexPath:0];
[fetchRequest release];
[sortDescriptor release];
}
精彩评论