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

Posted

技术标签:

【中文标题】如何查询按核心数据中唯一值分组的值?【英文标题】:How do I query for values grouped by unique values in core data? 【发布时间】:2015-12-03 01:47:09 【问题描述】:

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

假设我正在查询以获取患者名字或姓氏为“xyz”的托管对象(有多个“xyz”记录)。

如何获得一个唯一的“xyz”作为字典对象和重复的“xyz”托管对象作为该字典中的对象。

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

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

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

有人可以帮我解决这个问题吗?如果有什么不明白的地方我会提供更多的解释。

谢谢

【问题讨论】:

【参考方案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;


【讨论】:

以上是关于如何查询按核心数据中唯一值分组的值?的主要内容,如果未能解决你的问题,请参考以下文章

按名称分组的核心数据获取属性

核心数据 - 分组

如何将查询更改为核心数据(神奇记录)中的获取结果?

核心数据 NSFetchRequest 按类别排序方法返回值

SwiftUI:按核心数据属性分组和求和

MySQL核心面试技术(持续更新)