2
我有這些對象是唯一的,除了兩列我用作UITableView中的顯示。由於這個原因,UITableView通常會顯示重複的內容。我需要以某種方式過濾出這些重複項。過濾NSFetchedResultsController以刪除具有相同名稱的對象
在這種情況下,在FetchResult上設置distinctResult將不起作用,因爲這會限制NSFetchedResultsController的功能,並且它要求REsultType是NSDictionary而不是託管對象的子類。
沒有人有任何想法我可以如何使用謂詞過濾這些重複?請記住,除了其中兩個對象外,這些對象上的每個字段都是唯一的。
-(NSFetchedResultsController *) fetchGroupedObjects:(NSString *)entityDescription
sortField:(NSString *)sortField
withPredicate:(NSPredicate *)predicate {
BPRAppDelegate *delegate = (BPRAppDelegate *)[UIApplication sharedApplication].delegate;
NSManagedObjectContext *context = delegate.managedObjectContext;
//NSError *error;
//Fetch the data....
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:entityDescription inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSSortDescriptor *groupDescription = [[NSSortDescriptor alloc]
initWithKey:GROUP_NAME ascending:YES];
//Sort by Category Name
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:sortField ascending:YES];
NSMutableArray *sorts = [[[NSMutableArray alloc] init] autorelease];
[sorts addObject:sortDescriptor];
[sorts addObject:groupDescription];
[fetchRequest setSortDescriptors:sorts];
//[fetchRequest setResultType:NSDictionaryResultType];
//[fetchRequest setPropertiesToGroupBy:[entity.propertiesByName valueForKey:CONTRACTOR_NAME];
if (predicate != nil)
[fetchRequest setPredicate:predicate];
//NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
//NSFetchResultsController
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *fetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:context sectionNameKeyPath:GROUP_NAME
cacheName:nil]; //Don't use a cache
[fetchRequest release];
[sortDescriptor release];
[groupDescription release];
return fetchedResultsController; //You can't autorelease this thing... the requestor must do that.
}
我愛你來幫助你。但是我需要更多關於你想要過濾的實體的細節。實體稱爲什麼,它具有哪些領域 - 以及哪些領域是愚蠢的? – 2011-12-29 15:58:36