NSFetchResultsController 谓词问题

发布于 2024-12-02 07:42:04 字数 1972 浏览 0 评论 0原文

在此处输入图像描述

我在如何制定 NSPredicate 以获得正确数据时遇到问题。我有三个实体(报价、列表、项目)和一个与每对(列表、项目)的报价匹配的实体 OfferMatch。

我试图从选定的列表中获取所有报价,但不同的项目可以具有相同的报价,当我获取它们时,我会得到一些重复的报价。我希望得到明确的答案,而无需多次显示任何优惠。

我试图使用:

[NSFetchRequest setResultType:NSDictionaryResultType] 
[NSFetchRequest ssetPropertiesToFetch: 
[NSFetchRequest ssetReturnsDistinctResults:YES]

但 NSFetchResultsController 根本不支持 NSDictionaryResultType 。

这是我设置 NSFetchResultsController 时的示例代码:

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    // I'm interested in both Offer of OfferMAtch entity to get, so you can select more usefull for you 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Offer or OfferMatch" inManagedObjectContext:[CoreDataHandler context]];
    [request setEntity:entity];
    // I want to get all Offers from selected List, but with each one being only once
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@" ---- HERE IS MAGIC --- "];
    [request setPredicate:predicate];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"categoryID" ascending:NO];
    NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"descriptionString" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, sortDescriptor2, nil];
    [request setSortDescriptors:sortDescriptors];
    [sortDescriptors release];
    [sortDescriptor release];
    [sortDescriptor2 release];
    if (self=[[OfferFetchResults alloc] 
              initWithFetchRequest:request 
              managedObjectContext:[CoreDataHandler context] 
              sectionNameKeyPath:@"categoryID"
              cacheName:nil]) 
    {
        self.delegate = self;
    }
    [request release];
    [self performFetch:nil];

我不擅长制定复杂的 NSPredicates。我希望有人能帮助我,因为 NSPredicate 编程指南不会帮助我。

enter image description here

I'm having problem how to formulate NSPredicate to get correct data. I have three entities (Offer, List, Item) and an entity OfferMatch wchich matches an offer for each pair (List, Item).

I'm trying to get all offers from selected list, but different Items can have the same offer and when I fetch them I get some offers duplicated. I want to get distinct answer whithout any offers showed more than once.

I was trying to use:

[NSFetchRequest setResultType:NSDictionaryResultType] 
[NSFetchRequest ssetPropertiesToFetch: 
[NSFetchRequest ssetReturnsDistinctResults:YES]

but NSFetchResultsController is not supporting NSDictionaryResultType at all.

Here is a sample code when I set up NSFetchResultsController:

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    // I'm interested in both Offer of OfferMAtch entity to get, so you can select more usefull for you 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Offer or OfferMatch" inManagedObjectContext:[CoreDataHandler context]];
    [request setEntity:entity];
    // I want to get all Offers from selected List, but with each one being only once
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@" ---- HERE IS MAGIC --- "];
    [request setPredicate:predicate];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"categoryID" ascending:NO];
    NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"descriptionString" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, sortDescriptor2, nil];
    [request setSortDescriptors:sortDescriptors];
    [sortDescriptors release];
    [sortDescriptor release];
    [sortDescriptor2 release];
    if (self=[[OfferFetchResults alloc] 
              initWithFetchRequest:request 
              managedObjectContext:[CoreDataHandler context] 
              sectionNameKeyPath:@"categoryID"
              cacheName:nil]) 
    {
        self.delegate = self;
    }
    [request release];
    [self performFetch:nil];

I'm not good in formulating complex NSPredicates. I hope someone will help me, because NSPredicate Programming Guide wont' help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

小帐篷 2024-12-09 07:42:04

解决了...我不再尝试准备 NSPredicate。我简单地更改了数据库模型,删除实体 OfferMatch,然后直接在主要实体之间添加适当的关系。

Solved ... I quit trying to prepare NSPredicate. I simple changed DB model deleting entity OfferMatch and then adding appropriate relationships beetwen main Entities directly.

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