首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >CoreData -向现有实体添加新关系

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

Stack Overflow用户
提问于 2011-02-15 23:04:04
回答 2查看 824关注 0票数 1

我有以下基本问题:

我有两个实体,person和department。

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

具有新部门关系的简单插入:

代码语言:javascript
复制
    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];

最后一行就是链接--这没问题。

但是,如果我想在执行上面的代码后执行以下操作:

代码语言:javascript
复制
    [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上搜索或阅读几个小时的开发人员文档并不像我想的那样有帮助:)

对我来说,作为一个初学者,现在重要的是我是否在正确的道路上,有人能证实这一点吗?

感谢和问候,

马蒂亚斯

EN

Stack Overflow用户

发布于 2011-02-16 16:59:02

抱歉,检查函数如下所示:

代码语言:javascript
复制
- (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];
}
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5005289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档