2011-12-14 118 views
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. 

}

+0

我愛你來幫助你。但是我需要更多關於你想要過濾的實體的細節。實體稱爲什麼,它具有哪些領域 - 以及哪些領域是愚蠢的? – 2011-12-29 15:58:36

回答

0

雖然它可能是更容易建立一個可選的一對一關係回到本身(比如,「子」),這樣你就可以用NSPredicate其中「孩子取==無「,你可以使用[array enumerateObjectsUsingBlock:]或等價物在純粹的目標C中很容易地進行後置過濾,並且只按照您的標準將唯一對象添加到結果數組中。

相關問題