核心数据:返回另一个实体的对象的谓词
我的数据模型中有两个实体:Details
和 Lookup
。我需要找到与具有特定属性值的特定 Lookup
对象相关的所有 Details
对象,然后通过获取的结果控制器返回这些 Details
对象。
我的 NSManagedObjectSubclasses:
@interface Details : NSManagedObject {
@privateI
}
@property (nonatomic, retain) NSString * owner;
@property (nonatomic, retain) NSString * introduction;
@property (nonatomic, retain) NSString * id;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSString * created;
@property (nonatomic, retain) NSString * modified;
@property (nonatomic, retain) NSNumber * type;
@property (nonatomic, retain) NSString * desc;
@interface Lookup : NSManagedObject {
@private
}
@property (nonatomic, retain) NSDate * search_date;
@property (nonatomic, retain) NSString * search_phrase;
@property (nonatomic, retain) NSSet* searchResults;
我需要根据其 search_phrase
属性找到一个 Lookup
对象,然后获取所有相关的 Details
对象并返回获取的对象结果控制器。
我想我必须首先搜索 Lookup
对象,然后遍历 Detail 对象的 NSSet,但我不知道如何在 NSFetchedResultsController 中返回这些对象。
我已经尝试过:
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"search_phrase = %@", self.searchPhrase];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
fetchRequest.predicate = predicate;
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Lookup" inManagedObjectContext:self.context];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"search_phrase" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.context sectionNameKeyPath:nil cacheName:@"Searches"];
我有一个找到正确的 Lookup
对象的请求,但我不知道如何从获取的结果控制器获取相关的 Detail
对象。
I have two entities in my data model: Details
and Lookup
. I need find all Details
objects related to a specific Lookup
object that has specific attribute value and then return those Details
objects via a fetched results controller.
My NSManagedObjectSubclasses:
@interface Details : NSManagedObject {
@privateI
}
@property (nonatomic, retain) NSString * owner;
@property (nonatomic, retain) NSString * introduction;
@property (nonatomic, retain) NSString * id;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSString * created;
@property (nonatomic, retain) NSString * modified;
@property (nonatomic, retain) NSNumber * type;
@property (nonatomic, retain) NSString * desc;
@interface Lookup : NSManagedObject {
@private
}
@property (nonatomic, retain) NSDate * search_date;
@property (nonatomic, retain) NSString * search_phrase;
@property (nonatomic, retain) NSSet* searchResults;
I need to find a Lookup
object based on its search_phrase
attribute and then get all the related Details
objects and return those in a fetched results controller.
I think I have to search for the Lookup
object first, then walk the NSSet of Detail objects but I do not know how to return those in the NSFetchedResultsController.
I've tried:
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"search_phrase = %@", self.searchPhrase];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
fetchRequest.predicate = predicate;
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Lookup" inManagedObjectContext:self.context];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"search_phrase" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.context sectionNameKeyPath:nil cacheName:@"Searches"];
I have a request that finds the right Lookup
object but I don't know how to get the related Detail
objects from the fetched results controller.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您没有在
Details
和Lookup
之间定义相互关系,而只是定义到Details
的单向Lookup
代码>.您需要向从Detail
运行的数据模型实体添加到Lookup
的关系,并设置其具有Lookup.searchResults
的倒数和您的Detail
类需要一个类似于以下内容的属性:具有互惠关系可以让您通过从
Lookup
对象开始查找Detail
对象,并让您找到以 a 开头的Lookup
对象详细信息
对象。如果您希望 tableview 显示
Detail
对象列表,则需要配置获取结果控制器的获取请求以针对Detail
实体进行获取。根据经验,您始终将获取请求实体设置为您希望在表视图中显示其对象的实体。同样,谓词将针对
Detail
实体运行,因此您需要一个针对以Detail
实体的属性开头的键路径进行计算的谓词。在本例中,我们需要所有相关Lookup
对象具有等于提供值的search_phase
属性的Details
对象。所以:现在这样设置您的获取请求:
将此获取提供给您的 NSFetchedResultsController ,它将返回您正在查找的
Detail
对象。总之:
Firstly, you don't have a reciprocal relationship defined between
Details
andLookup
but just a one-wayLookup
toDetails
. You need to add a relationship to both the data model entity running fromDetail
toLookup
and have it set has the recipocal ofLookup.searchResults
and yourDetail
class needs a property something like:Having a reciprocal relationship lets you find
Detail
objects by starting with aLookup
object and lets you find aLookup
object starting with aDetail
object.If you want you tableview to display a list of
Detail
objects, then you need to configure the fetched results controller's fetch request to fetch against theDetail
entity. As a rule of thumb, you always set the fetch request entity to the entity whose objects you wish to display in the tableview.The predicate likewise will be run against the
Detail
entity so you need a predicate that evaluates against a keypath that starts with a property of theDetail
entity. In this case, we want allDetails
objects whose relatedLookup
object has asearch_phase
attribute equalling a provide value. So:Now setup your fetch request thusly:
Give this fetch to your NSFetchedResultsController and it will return the
Detail
objects you are looking for.In summary:
如果没有一些示例代码,甚至没有实际的关系,很难回答,但这里是:
AND #Put name of thelation here# IN (the results of 1)
查询表 B虽然还有其他需要添加的内容,但您应该停止将核心数据视为关系数据库。这是一个对象图。核心数据可能会也可能不会使用不同的表来存储数据。您应该关心的是对象及其关系。
如果我理解正确的话,你真的不需要一对多,而是多对多。
然后,您将能够通过对 B 对象的查询同时执行这两个查询:
Hard to answer without some of your sample code or even the actual relationships, but here goes:
AND #Put name of the relationship here# IN (the results of 1)
Something else to add though, you should stop thinking about core data as a relational database. It's an object graph. Core data may or may not use different tables to store the data. What you should be concerned with are the objects and their relationships.
If I understand correctly you don't really want a 1 to many, but a many to many.
You will then be able to perform both queries at once with a query for B objects: