NSFetchResultsController 谓词问题
我在如何制定 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 编程指南不会帮助我。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了...我不再尝试准备 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.