我有以下基本问题:
我有两个实体,人和部门。
在添加我要检查的新人之前,该部门尚未存在,如果存在,则将新人员链接到现有部门。
使用新的部门关系进行简单插入:
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];
简而言之,我的问题是表部门的重复条目。
也许有人知道一个好的CoreData教程,这对于具有高级SQL知识的初学者是有益的。 (在Google上搜索或阅读开发人员文档数小时并没有那么有用,因为我认为:))
对我来说,作为初学者,现在重要的是我是否采用正确的方式,有人可以证实这一点吗?
谢谢和问候,
的Matthias
答案 0 :(得分:0)
您在if/else
语句中定义了部门变量,然后在其外部使用了另一个变量。尝试将if
更改为:
[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";
}
虽然没有看到checkForExistingDepartment的代码,但我们无法帮到你。 。
答案 1 :(得分:0)
对不起,检查功能如下:
- (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];
}