如何查询核心数据中按唯一值分组的值?

时间:2015-12-03 01:47:10

标签: ios objective-c core-data

Core Data Model

考虑具有多对多关系的上述模型。

让我们假设我正在查询托管对象,其中患者名字或姓氏是" xyz" (有多个记录" xyz")。

如何获得独特的" xyz"作为字典对象并重复" xyz" Managed-objets作为该字典中的对象。

到目前为止,我能够获得不同的值,而且我能够获得托管对象的数组,其中" xyz"是谓词字符串。

但我无法想到一种方法来获取字典数组及其对象作为重复值的托管对象。

我希望有一个具有唯一值的字典数组作为字典名称,字典中的objets应该是托管对象/字典。

请有人帮我解决这个问题。如果不理解任何部分,我会提供更多解释。

由于

1 个答案:

答案 0 :(得分: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;

}