【问题标题】:How to get distinct result based on one field from core data如何根据核心数据中的一个字段获得不同的结果
【发布时间】:2017-05-30 14:53:36
【问题描述】:

我的核心数据表是这样的:

我想收到每个发件人的最新消息。所以结果应该是这样的:

这是我的代码:

    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"MyCoreDataObject" inManagedObjectContext:moc];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"MESSAGE <> ''"];

    request.predicate = predicate;
    NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"TIME" ascending:NO];
    NSArray *sortDescriptors = @[sd1];
    [request setSortDescriptors:sortDescriptors];
    [request setEntity:entityDescription];
    NSError *error;
    NSArray *msgs = [moc executeFetchRequest:request error:&error];

我将获得所有排序的数据(最新消息优先)。 但是如何根据发件人获得唯一值?所以只有每个发件人的最新消息?

基本上我需要这个 SQL 的等价物:

SELECT message, sender, MAX(time) as maxTime FROM myTable GROUP BY sender;

【问题讨论】:

    标签: ios objective-c core-data nspredicate


    【解决方案1】:

    获取消息字段是困难的部分。

    基本上,如果您使用 group by,您只能获取 group by (sender) 中的属性和带有表达式 (time) 的属性。

    要获取所有其他值,您需要使用获取的值(发送者和时间)并执行另一个获取以从核心数据中获取它。

    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"MyCoreDataObject" inManagedObjectContext:moc];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"message <> ''"];
    
    request.predicate = predicate;
    NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"time" ascending:NO];
    NSArray *sortDescriptors = @[sd1];
    [request setSortDescriptors:sortDescriptors];
    [request setEntity:entityDescription];
    
    NSExpressionDescription *timestampExp = [[NSExpressionDescription alloc] init];
    [timestampExp setExpression:[NSExpression expressionWithFormat:@"@max.time"]];
    [timestampExp setName:@"time"];
    [timestampExp setExpressionResultType:NSDateAttributeType];
    
    [request setSortDescriptors:sortDescriptors];
    [request setEntity:entityDescription];
    [request setResultType:NSDictionaryResultType];
    [request setPropertiesToGroupBy:[NSArray arrayWithObject:@"sender"]];
    [request setPropertiesToFetch:[NSArray arrayWithObjects:@"sender", timestampExp, nil]];
    
    NSError *error;
    NSArray *msgs = [moc executeFetchRequest:request error:&error];
    for (NSDictionary *msg in msgs) {
        request = [[NSFetchRequest alloc] init];
        predicate = [NSPredicate predicateWithFormat:@"message <> '' and sender = %@ and time = %@ ", msg[@"sender"], msg[@"time"]];
    
        request.predicate = predicate;
        [request setSortDescriptors:sortDescriptors];
        [request setEntity:entityDescription];
        NSDictionary *myMessage = [moc executeFetchRequest:request error:&error][0];
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-31
      • 1970-01-01
      • 1970-01-01
      • 2018-06-19
      • 2013-08-22
      • 1970-01-01
      • 1970-01-01
      • 2015-03-18
      相关资源
      最近更新 更多