如何创建基于属性和关系过滤Core Data对象的NSFetchRequest? [英] How to create an NSFetchRequest which filters Core Data objects based on attributes AND relationships?
问题描述
我有一个Core Data模型设置如下:
I have a Core Data model setup like so:
- Blockbuster 实体
- 与 DVD 实体有许多关系。
- Blockbuster Entity
- To-Many relationship to DVD entities.
- 标题属性(字符串)
- 与父级 Blockbuster 实体的一对一关系
- title attribute (string)
- To-One relationship to a parent Blockbuster entity
一个大片可以有多个DVD里面(To-Many)。每个DVD只能是一个Blockbuster(To-One)的一部分。
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的。例如, 可能有一个变形金刚2的副本, 和 让我们假设没有Blockbuster有同一个标题电影的多个副本。
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 A might have a copy of "Transformers 2", and so does Blockbuster C and Blockbuster G. Let's pretend no Blockbuster has more than one copy of the same titled movie.
我有兴趣从变形金刚2 > Blockbuster C 。我不想从 Blockbuster A 或 G 复制,因为我的老板经常在那里,我只能在燃烧这块垃圾时离开。
I'm interested in grabbing the Transformers 2 DVD from Blockbuster C. I don't want the copies from Blockbuster A or G, because my boss frequents there, and I can only get away with burning this piece of garbage in Blockbuster C.
我的问题是,如何形成NSFetchRequest, / strong>与标题Transformers 2( Blockbuster C父实体的子级)之间的关系?
My question is, how do I form an NSFetchRequest which grabs a DVD with the title "Transformers 2", which is a child of the Blockbuster "C" parent entity?
此类型的请求将抓取所有Transformer 2DVD,但不是特定于Blockbuster C实体的请求:
This type of request will grab all the "Transformer 2" DVD's, but not the one specific to the Blockbuster C entity:
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];
有一种方法可以传递,比如说,NSPredicate中相关对象的Object ID,基于<?p>
Is there a way to pass, say, the Object ID of a related object inside the NSPredicate, filtering based on that?
推荐答案
可以遍历
NSPredicate
中的关系。例如,您可以编写You can traverse relationships in an
NSPredicate
. For example, you could write something like[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]
完整语法记录在这里。但
setOfBlockbusters
可以是一个集合,一个数组或一个字典(如果它是一个字典,值,而不是键)。The full syntax is documented here. But
setOfBlockbusters
could be a set, an array, or a dictionary (if it's a dictionary, the values, not the keys, are used).这篇关于如何创建基于属性和关系过滤Core Data对象的NSFetchRequest?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!