xcode 特定关系的核心数据 NSFetchRequest?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5333943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:32:35  来源:igfitidea点击:

Core Data NSFetchRequest for Specific Relationship?

objective-cxcodecore-dataentity-relationship

提问by Craig Otis

I'm transitioning an existing data model that was previously stored in XML to Core Data, so I'm trying to learn the ropes as properly as possible. Core Data is obviously one of those technologies that isn't going anywhere anytime soon, so I might as well "learn it right."

我正在将以前存储在 XML 中的现有数据模型转换为 Core Data,因此我正在尝试尽可能正确地学习绳索。Core Data 显然是那些短期内不会流行的技术之一,所以我不妨“正确学习”。

Take for example a Core Data model with two Entities:

以具有两个实体的核心数据模型为例:

  1. Person
  2. Food
  1. 食物

Personhas 2 one-to-many relationships with Food:

PersonFood有 2 个一对多的关系:

  1. favoriteFoods (1-to-many)
  2. hatedFoods (1-to-many)
  1. 最喜欢的食物(一对多)
  2. 讨厌的食物(一对多)

(Both Person and Food are subclasses of NSManagedObject as well.)

(Person 和 Food 都是 NSManagedObject 的子类。)

In the previous data model, Personmaintained two NSArrayinstance variables. If I wanted favorite foods, I could call:

在之前的数据模型中,Person维护了两个NSArray实例变量。如果我想要最喜欢的食物,我可以打电话:

Person *fred = [[Person alloc] init];    
NSArray *fredsFavorites = fred.favoriteFoods;

Easy squeezy.

容易挤。

I'm reading through the documentation for Core Data, and I can't seem to find the right way to obtain this NSArray given an NSFetchRequest, because I can't define which relationship I want to obtain objects from.

我正在阅读 Core Data 的文档,但我似乎无法找到正确的方法来获取给定的 NSArray NSFetchRequest,因为我无法定义我想从中获取对象的关系。

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Food" inManagedObjectContext:[fred managedObjectContext]]];
[request setIncludesSubentities:NO];
NSArray *fredsFavoriteAndHatedFoods = [[fred managedObjectContext] executeFetchRequest:request error:nil];

This returns all of the Fooditems stored in both favoriteFoodsand hatedFoods. How can I split these up? Surely there's a simple explanation, but I don't currently grasp the concept well enough to explain it in Core Data jargon, thus my Google searches are fruitless.

这将返回存储在和 中的所有Food项目。我怎样才能把这些分开?当然有一个简单的解释,但我目前没有很好地掌握这个概念,无法用 Core Data 术语解释它,因此我的谷歌搜索没有结果。favoriteFoodshatedFoods

回答by Nick Forge

The most straightforward way to get it is to simply access the relationship NSSetdirectly:

获得它的最直接的方法是直接访问关系NSSet

NSArray *fredsFavorites = [fred.favoriteFoods allObjects];

(I've shown how to get an NSArrayfrom the resulting NSSet).

(我已经展示了如何NSArray从结果中获取NSSet)。

Alternatively, if you haven inverse relationship set up (which you should) you could use a fetch request like this:

或者,如果您设置了反向关系(您应该这样做),您可以使用这样的获取请求:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Food" inManagedObjectContext:[fred managedObjectContext]]];
[request setPredicate:[NSPredicate predicateWithFormat:@"ANY personsWithThisAsFavorite == %@", fred]];
NSArray *fredsFavoriteFoods = [[fred managedObjectContext] executeFetchRequest:request error:nil];

This assumes that personsWithThisAsFavoriteis the inverse relationship to favoriteFoods. Unless I'm reading your example wrong, favoriteFoodsshould really be a many-to-many relationship, since a person can have multiple favorite foods, and a food can have multiple people with that food as a favorite.

这假设personsWithThisAsFavorite与 成反比favoriteFoods。除非我读错了你的例子,favoriteFoods否则应该是多对多的关系,因为一个人可以有多种喜欢的食物,而一种食物可以有多个人喜欢这种食物。

(Note that I haven't tested this, so the NSPredicatemight not be 100% correct)

(请注意,我尚未对此进行测试,因此NSPredicate可能不是 100% 正确)