CoreData - 向现有实体添加新关系
我有以下基本问题:
我有两个实体,人员和部门。
在添加新人员之前,我想检查该部门是否尚不存在,如果存在,则将新人员链接到现有部门。
简单插入新的部门关系:
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 知识的初学者很有帮助。 (在谷歌上搜索或阅读开发人员文档几个小时并没有我想象的那么有帮助:))
对于我作为初学者来说,现在重要的是我是否走在正确的道路上,有人可以证实这一点吗?
谢谢和问候,
马蒂亚斯
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";
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];
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在
if/else
语句内定义了 Department 变量,然后在其外部使用不同的变量。尝试将您的if
更改为如下所示:虽然没有看到 checkForExistingDepartment 的代码,但我们无法为您提供太多帮助。 。 。
You're defining the department variable inside the
if/else
statement and then using a different one outside of it. Try changing yourif
to look like :Though without seeing the code for checkForExistingDepartment we can't help you very much . . .
抱歉,检查功能如下所示:
Sorry, the check function looks like this: