objective-c 如何创建基于属性和关系过滤核心数据对象的 NSFetchRequest?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1527966/
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-03 22:19:22  来源:igfitidea点击:

How to create an NSFetchRequest which filters Core Data objects based on attributes AND relationships?

objective-ccocoacore-dataentity-relationship

提问by Dave Gallagher

I have a Core Data model setup like so:

我有一个像这样的核心数据模型设置:

  • BlockbusterEntity
    • To-Many relationship to DVDentities.
  • DVDEntity
    • titleattribute (string)
    • To-One relationship to a parent Blockbusterentity
  • 重磅实体
    • DVD实体的多对多关系。
  • DVD实体
    • 标题属性(字符串)
    • 与父Blockbuster实体的一对一关系

A single Blockbuster can have multiple DVD's inside of it (To-Many). Each DVD can only be part of a single Blockbuster (To-One).

一个 Blockbuster 里面可以有多个 DVD(多对多)。每张 DVD 只能是一个 Blockbuster (To-One) 的一部分。

There are multiple Blockbuster's, and some may contain DVD entities which share the same title as DVD entities from other Blockbuster's. For example, Blockbuster Amight have a copy of "Transformers 2", and so does Blockbuster Cand Blockbuster G. Let's pretend no Blockbuster has more than one copy of the same titled movie.

有多个 Blockbuster,其中一些可能包含与其他 Blockbuster 的 DVD 实体共享相同标题的 DVD 实体。例如,Blockbuster A可能有“变形金刚 2”的副本,Blockbuster CBlockbuster G也是如此。让我们假设没有 Blockbuster 拥有多于一份同名电影的副本。

I'm interested in grabbing the Transformers 2DVD from Blockbuster C. I don't want the copies from Blockbuster Aor G, because my boss frequents there, and I can only get away with burning this piece of garbage in Blockbuster C.

我有兴趣从Blockbuster C中获取变形金刚 2DVD 。我不想要Blockbuster AG的副本,因为我的老板经常去那里,而我只能在Blockbuster C中烧掉这块垃圾。



My question is, how do I form an NSFetchRequest which grabs a DVDwith the title"Transformers 2", which is a child of the Blockbuster"C" parent entity?

我的问题是,我怎么形成NSFetchRequest它抓起DVD标题“变形金刚2”,这是一个孩子一鸣惊人“C”父实体?

This type of request will grab all the "Transformer 2" DVD's, but not the one specific to the Blockbuster C entity:

此类请求将获取所有“变形金刚 2”DVD,但不会获取特定于 Blockbuster C 实体的 DVD:

NSManagedObjectContext *moc = [self managedObjectContext];

NSString *aTitle = @"Transformers 2";
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title == %@", aTitle];
[request setEntity:[NSEntityDescription entityForName:@"DVD" inManagedObjectContext:moc]];
[request setPredicate:predicate];

NSError *error = nil;
NSArray *results = [moc executeFetchRequest:request error:&error];

// error handling code
[request release];

Is there a way to pass, say, the Object ID of a related object inside the NSPredicate, filtering based on that?

有没有办法在 NSPredicate 中传递相关对象的对象 ID,并以此为基础进行过滤?

回答by Alex

You can traverse relationships in an NSPredicate. For example, you could write something like

您可以遍历NSPredicate. 例如,您可以编写类似

[NSPredicate predicateWithFormat:@"title == %@ AND blockbuster.name LIKE \"Blockbuster C\"", @"Transformers 2"]

Now, if you don't have a property to compare against and you need to check actual objects, then you could use something like

现在,如果您没有要比较的属性并且需要检查实际对象,那么您可以使用类似

[NSPredicate predicateWithFormat:@"title == %@ AND blockbuster IN %@", @"Transformers 2", setOfBlockbusters]

The full syntax is documented here. But setOfBlockbusterscould be a set, an array, or a dictionary (if it's a dictionary, the values, not the keys, are used).

完整的语法记录在此处。但setOfBlockbusters可以是一个集合、一个数组或一个字典(如果它是一个字典,将使用值,而不是键)。