【问题标题】:Fetch one-to-many relationship fault still shows after adding setRelationshipKeyPathsForPrefetching添加 setRelationshipKeyPathsForPrefetching 后仍然显示获取一对多关系错误
【发布时间】:2014-06-04 04:49:06
【问题描述】:

假设我的数据模型有两个实体:Department 和 Person。 Departments 与 Person 有一个称为“departmentToPerson”的对多关系。 Person 与 Department 具有称为“personToDepartment”的一对一关系。

我想填充属于选定部门的一组人员。为了选择部门,我创建了一个 UILabel,它显示了从弹出表视图中选择的部门名称。当我运行应用程序时,日志显示:

personToDepartment.departmentName ==(实体: 部门;编号:0x8cc1920 ; 数据: { 部门名称 = 市场营销; departmentToPerson = "";

我已经阅读了错误的目的并实现了 setRelationshipKeyPathsForPrefetching,但我仍然遇到了错误。我会声明我是新手,可能会遗漏一些明显的东西。当我删除谓词时,我的表视图会填充所有 personName。代码如下:

- (NSFetchedResultsController *)fetchedResultsController {

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                entityForName:@"Person" inManagedObjectContext:_managedObjectContext];
    [fetchRequest setEntity:entity];


    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"personToDepartment.departmentName = %@", selectedDepartment];
    NSLog(@"predicate is: %@",predicate);

    [fetchRequest setPredicate:predicate];

    [fetchRequest setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects: @"departmentToPerson", nil]];

fetchRequest.returnsObjectsAsFaults = NO;

    NSSortDescriptor *sort = [[NSSortDescriptor alloc]
                    initWithKey:@"personName" ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];


    [fetchRequest setFetchBatchSize:20];


    NSFetchedResultsController *theFetchedResultsController =
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
            managedObjectContext:_managedObjectContext sectionNameKeyPath:nil
                cacheName:nil];
    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;

    return _fetchedResultsController;

}

再次,我从 UILabel 中选择了一个新部门,日志显示 selectedDepartment,但随后显示关系错误并且不填充表。

提前感谢您的帮助!

6 月 9 日更新

我也发现这很有效:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"personToDepartment = %@", selectedDepartment];

【问题讨论】:

    标签: core-data relationship fetch predicate fault


    【解决方案1】:

    我也是 CoreData 的新手,但在我看来谓词是错误的。根据日志记录,您即将将personToDepartment.departmentName 与部门对象进行比较。谓词应如下所示: NSPredicate *predicate = [NSPredicate predicateWithFormat:@"personToDepartment.departmentName = %@", selectedDepartment.departmentName];

    但是有更好的方法。 selectedDepartment.departmentToPerson 将返回一个包含所有属于该部门的人的 NSSet(如果之前设置了关系)。但是警告,如果你尝试NSLog(@"%@", selectedDepartment.departmentToPerson),你可能会得到“relationship fault”,因为 CoreData 不会进行获取,直到你处理 NSSet 中的特定对象或通过它枚举。但例如NSLog(@"%d", selectedDepartment.departmentToPerson.count) 将强制CoreData 进行提取,您将看到部门中的人数。

    更新: NSSet 为空可能是因为您在创建人员对象时没有设置关系。将自动设置部门到人员的反向多对多关系。

    - (id)insertNewPerson:(NSDictionary *)personInfo inDepartment:(Department*)departmentObject 
    {
        Person *personObject = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];
        personObject.name = personInfo.name;
        personObject.departmentName = departmentObject.name;
        ...
        personObject.personToDepartment = departmentObject;   //this is RELATIONSHIP, if set, then you can access all persons from specific department by doing: departmentObject.departmentToPerson
    }
    

    【讨论】:

    • bucherland 这正是它所需要的,谢谢!我花了很多时间来解决这个问题。我会接受,因为它回答了我的第一个问题。现在,我用谓词尝试了 NSSet 方法,但没有返回任何内容。我还需要为此添加更多内容吗?用于计数的 NSLog 确实有效。
    • 很高兴它有帮助。 selectedDepartment.departmentToPerson 不起作用,因为您在创建 Person 时可能没有设置关系。 (NSLog 值为空)。
    猜你喜欢
    • 1970-01-01
    • 2014-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    相关资源
    最近更新 更多