我有以下基本问题:
我有两个实体,person和department。
在添加新人员之前,我要检查部门是否不存在,如果存在,则将新人员链接到现有部门。
具有新部门关系的简单插入:
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";
person1.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];最后一行就是链接--这没问题。
但是,如果我想在执行上面的代码后执行以下操作:
[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];简而言之,我的问题是表department中的重复条目。
也许有人知道一个好的CoreData教程,这对具有高级SQL知识的初学者来说是很好的。(在google上搜索或阅读几个小时的开发人员文档并不像我想的那样有帮助:)
对我来说,作为一个初学者,现在重要的是我是否在正确的道路上,有人能证实这一点吗?
感谢和问候,
马蒂亚斯
发布于 2011-02-16 16:59:02
抱歉,检查函数如下所示:
- (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];
}https://stackoverflow.com/questions/5005289
复制相似问题