NSFetchResultsController predicate problem
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开发者_StackOverflow 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.
Solved ... I quit trying to prepare NSPredicate. I simple changed DB model deleting entity OfferMatch and then adding appropriate relationships beetwen main Entities directly.
精彩评论