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

Posted

技术标签:

【中文标题】CoreData - 向现有实体添加新关系【英文标题】:CoreData - Adding a new relationship to existing entity 【发布时间】:2011-02-15 15:04:03 【问题描述】:

我有以下基本问题:

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

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

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

    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 知识的初学者。 (在谷歌上搜索或阅读开发者文档几个小时并没有我想的那么有用:))

对于作为初学者的我来说,重要的是现在我是否走在正确的道路上,有人可以确认这一点吗?

谢谢你,

马蒂亚斯

【问题讨论】:

【参考方案1】:

您在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 的代码,但我们帮不了你太多。 . .

【讨论】:

【参考方案2】:

对不起,检查功能是这样的:

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

【讨论】:

以上是关于CoreData - 向现有实体添加新关系的主要内容,如果未能解决你的问题,请参考以下文章

CoreData 向实体行添加新关系

向 coredata 添加新实体 - 我还能使用轻量级迁移吗?

在 Core Data 迁移期间添加关系 - 自我参考

在 Core Data 中替换多对多关系中的实体

Core Data + iCloud,更改通知插入和删除关系中的对象但不更新关系中现有实体的属性

如何检查关系是不是已建立 - Core Data