2015-12-03 62 views
1

Core Data Model如何查詢核心數據中按唯一值分組的值?

考慮上述模型與多對多關係。

我們假設我正在查詢患者姓名是「xyz」的受管理對象(有多個「xyz」記錄)。

如何獲得一個獨特的「xyz」作爲字典對象並重復「xyz」託管對象作爲該字典中的對象。

到目前爲止,我能夠獲得不同的值,並且我能夠獲得託管對象數組,其中「xyz」是謂詞字符串。

但我無法想出一種方法來獲取其對象的數組作爲重複值的託管對象。

我想擁有一個具有唯一值的字典數組,因爲字典名稱和字典中的objets應該是managed-objects/dictionaries。

請問有人可以幫我解決這個問題。如果任何部分不被理解,我會提供更多解釋。

謝謝

回答

1
#pragma mark - Search Patients By Patient Name 

-(NSArray*)GetBillsForPatientwith:(NSString*)name{ 

NSMutableArray *AllBillsData = [[NSMutableArray alloc]init]; 

NSArray *_strings = [self GetSplitStrings:name]; 

NSManagedObjectContext *context = [CoreDataManager GetCoreDataManager].managedObjectContext; 
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:_Claims inManagedObjectContext:context]; 
[fetchRequest setEntity:entity]; 

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"patient_FirstName" ascending:YES]; 
NSArray *sortDescriptors = @[sortDescriptor]; 
[fetchRequest setSortDescriptors:sortDescriptors]; 
NSMutableArray *subPredicates = [NSMutableArray array]; 


for (NSString *strings in _strings) { 
    [subPredicates addObject: 
    [NSPredicate predicateWithFormat:@"patient_FirstName CONTAINS[cd] %@ OR patient_LastName contains[cd] %@",strings,strings]]; 
} 
NSCompoundPredicate *predicate_ = [[NSCompoundPredicate alloc]initWithType:NSOrPredicateType subpredicates:subPredicates]; 
[fetchRequest setReturnsObjectsAsFaults:NO]; 
[fetchRequest setPredicate:predicate_]; 
fetchRequest.returnsDistinctResults = YES; 
fetchRequest.resultType = NSDictionaryResultType; 

fetchRequest.propertiesToFetch = [NSArray arrayWithObjects:[[entity propertiesByName] objectForKey:@"patient_FirstName"],[[entity propertiesByName] objectForKey:@"patient_LastName"], nil]; 
//fetchRequest.propertiesToGroupBy = [NSArray arrayWithObjects:[[entity propertiesByName] objectForKey:@"patient_FirstName"],[[entity propertiesByName] objectForKey:@"patient_LastName"], nil]; 

NSError *error; 
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; 
if (fetchedObjects == nil) { 
    // Handle the error. 
} 

for (NSDictionary *names in fetchedObjects) { 
    // [AllBillsData addObject:names]; 
    [AllBillsData addObject:[self GetCorrespondingClaimsForNames:names]]; 
} 
return AllBillsData; 
} 
//Entangled Method 
-(NSArray*)GetCorrespondingClaimsForNames:(id)Names{ 
    NSManagedObjectContext *context = [CoreDataManager 
GetCoreDataManager].managedObjectContext; 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:_Claims inManagedObjectContext:context]; 
    [fetchRequest setEntity:entity]; 

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"patient_FirstName" ascending:YES]; 
    NSArray *sortDescriptors = @[sortDescriptor]; 
    [fetchRequest setSortDescriptors:sortDescriptors]; 
    NSMutableArray *subPredicates = [NSMutableArray array]; 
    [subPredicates addObject: 
    [NSPredicate predicateWithFormat:@"patient_FirstName ==[cd] %@ AND patient_LastName ==[cd] %@",[Names valueForKey:@"patient_FirstName"],[Names valueForKey:@"patient_LastName"]]]; 

    NSCompoundPredicate *predicate_ = [[NSCompoundPredicate alloc]initWithType:NSOrPredicateType subpredicates:subPredicates]; 
    [fetchRequest setReturnsObjectsAsFaults:NO]; 
    [fetchRequest setPredicate:predicate_]; 

    NSError *error; 
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; 
    if (fetchedObjects == nil) { 
     // Handle the error. 
    } 

    return fetchedObjects; 

}