核心数据:返回另一个实体的对象的谓词

发布于 2024-11-18 16:38:58 字数 1972 浏览 6 评论 0原文

我的数据模型中有两个实体:DetailsLookup。我需要找到与具有特定属性值的特定 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱格式化 2024-11-25 16:38:58

首先,您没有在 DetailsLookup 之间定义相互关系,而只是定义到 Details 的单向 Lookup代码>.您需要向从 Detail 运行的数据模型实体添加到 Lookup 的关系,并设置其具有 Lookup.searchResults 的倒数和您的 Detail 类需要一个类似于以下内容的属性:

@property (nonAtomic,retain) Lookup *lookup;

具有互惠关系可以让您通过从 Lookup 对象开始查找 Detail 对象,并让您找到以 a 开头的 Lookup 对象详细信息对象。

如果您希望 tableview 显示 Detail 对象列表,则需要配置获取结果控制器的获取请求以针对 Detail 实体进行获取。根据经验,您始终将获取请求实体设置为您希望在表视图中显示其对象的实体。

同样,谓词将针对 Detail 实体运行,因此您需要一个针对以 Detail 实体的属性开头的键路径进行计算的谓词。在本例中,我们需要所有相关 Lookup 对象具有等于提供值的 search_phase 属性的 Details 对象。所以:

NSPredicate *p=[NSPredicate predicateWithFormat:@"lookup.search_phrase==%@", self.searchPhrase];

现在这样设置您的获取请求:

  NSFetchRequest *fetch=[[NSFetchRequest alloc] init];
  NSEntityDescription *detailsEntity=[NSEntityDescription entityForName:@"Details" inManagedObjectContext:self.context];
  [fetch setEntity:detailsEntity];
  [fetch setPredicate:p];
  //.. set up a sort however you want the details to appear in the tableview

将此获取提供给您的 NSFetchedResultsController ,它将返回您正在查找的 Detail 对象。

总之:

  • 始终在数据模型中使用互惠关系,这样您就可以获得灵活性。
  • 使用获取结果控制器时,将获取请求的实体设置为您希望其对象出现在表视图中的实体。

Firstly, you don't have a reciprocal relationship defined between Details and Lookup but just a one-way Lookup to Details. You need to add a relationship to both the data model entity running from Detail to Lookup and have it set has the recipocal of Lookup.searchResults and your Detail class needs a property something like:

@property (nonAtomic,retain) Lookup *lookup;

Having a reciprocal relationship lets you find Detail objects by starting with a Lookup object and lets you find a Lookup object starting with a Detail 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 the Detail 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 the Detail entity. In this case, we want all Details objects whose related Lookup object has a search_phase attribute equalling a provide value. So:

NSPredicate *p=[NSPredicate predicateWithFormat:@"lookup.search_phrase==%@", self.searchPhrase];

Now setup your fetch request thusly:

  NSFetchRequest *fetch=[[NSFetchRequest alloc] init];
  NSEntityDescription *detailsEntity=[NSEntityDescription entityForName:@"Details" inManagedObjectContext:self.context];
  [fetch setEntity:detailsEntity];
  [fetch setPredicate:p];
  //.. set up a sort however you want the details to appear in the tableview

Give this fetch to your NSFetchedResultsController and it will return the Detail objects you are looking for.

In summary:

  • Always use reciprocal relationships in a data model so you can have flexibility.
  • When using a fetched results controller, set the entity of the fetch request to the entity whose objects you wish to appear in the tableview.
童话 2024-11-25 16:38:58

如果没有一些示例代码,甚至没有实际的关系,很难回答,但这里是:

  • 对表 A 执行初始过滤。这将为您提供一组包含多个对象的结果。
  • 使用过滤器和 AND #Put name of thelation here# IN (the results of 1) 查询表 B

虽然还有其他需要添加的内容,但您应该停止将核心数据视为关系数据库。这是一个对象图。核心数据可能会也可能不会使用不同的表来存储数据。您应该关心的是对象及其关系。

如果我理解正确的话,你真的不需要一对多,而是多对多。
然后,您将能够通过对 B 对象的查询同时执行这两个查询:

A.your.a.query == 'What you're querying for' AND your.b.query == here

Hard to answer without some of your sample code or even the actual relationships, but here goes:

  • Perform your initial filter on table A. This gives you a set of results containing a number of objects.
  • Query table B with your filter and 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:

A.your.a.query == 'What you're querying for' AND your.b.query == here
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文