OGeek|极客世界-中国程序员成长平台

标题: iphone - 如何在核心数据中检索实体的唯一关系 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:52
标题: iphone - 如何在核心数据中检索实体的唯一关系

必须有更简单的方法来做到这一点。我有搜索和搜索,但似乎无法找到我正在寻找的答案。

假设我们有这样的关系 实体A<-->>实体B

如果我创建 2 个 EntityA 实例,每个实例有 3 个 Entity B 实例。

在显示每个 EntityA 的所有 EntityB 的 View Controller 中,它显示所有 6 个而不是与之相关的 3 个。

我可以让它正确显示的唯一方法是如果我将指针从一个 Controller 传递到另一个:

viewController2.entityA = viewController1.entityA;

然后按以下方式检索结果:

    NSMutableArray *result = [[NSMutableArray alloc] initWithArray:[entityA.entityBs allObjects]];

我的印象是,如果您最初获取父实体,则后续获取将基于该父实体,而不是全部返回。

任何帮助将不胜感激。



Best Answer-推荐答案


尝试以下方法:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *weightEntity = [NSEntityDescription entityForName"EntityB" inManagedObjectContext:[[yourCoreDataManager sharedInstance] managedObjectContext]];
[fetchRequest setEntity:weightEntity];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat"EntityA.name LIKE %@", @"EntityAName"]];

NSError *error = nil;
NSArray *result = [[yourCoreDataManager sharedInstance] managedObjectContext] executeFetchRequest:fetchRequest error:&error];

这样,您只请求属于给定实体 A 的那些实体 B。

关于iphone - 如何在核心数据中检索实体的唯一关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8734726/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4