CoreData - 向现有实体添加新关系

发布于 2024-10-17 15:15:24 字数 2235 浏览 3 评论 0原文

我有以下基本问题:

我有两个实体,人员和部门。

在添加新人员之前,我想检查该部门是否尚不存在,如果存在,则将新人员链接到现有部门。

简单插入新的部门关系:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

思念满溢 2024-10-24 15:15:24

您在 if/else 语句内定义了 Department 变量,然后在其外部使用不同的变量。尝试将您的 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 的代码,但我们无法为您提供太多帮助。 。 。

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 . . .

避讳 2024-10-24 15:15:24

抱歉,检查功能如下所示:

- (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];
}

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文