NSDictionaryResultType 表达式不考虑新插入的对象 [英] NSDictionaryResultType expression not taking into account newly inserted objects

查看:12
本文介绍了NSDictionaryResultType 表达式不考虑新插入的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要核心数据中某个字段的最大值,所以我设置了以下请求:

I want the maximum value of a field in core data so I have set up the following request:

// Create fetch
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
[fetch setEntity:[NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:context]];
[fetch setResultType:NSDictionaryResultType];

// Expression for Max ID
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"myNumbericID"];
NSExpression *minExpression = [NSExpression expressionForFunction:@"max:" arguments:[NSArray arrayWithObject:keyPathExpression]];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName:@"maxID"];
[expressionDescription setExpression:minExpression]; 
[expressionDescription setExpressionResultType:NSDoubleAttributeType];
[fetch setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];

// Execute the fetch. 
double theID = 0;
NSError *error;
NSArray *objects = [context executeFetchRequest:fetch error:&error]; 
if (objects && [objects count] > 0) { 
    theID = [((NSNumber *)[[objects objectAtIndex:0] valueForKey:@"maxID"]) doubleValue];
}

然而,它似乎没有考虑任何事先插入上下文中的新对象.这是它应该如何工作?它只适用于商店中的物品吗?

However it would appear it's not taking into account any new objects that have been inserted into the context before hand. Is this how it is supposed to work? Does it only work on objects in the store?

我在文档中找不到任何对它的引用.

I can't find any reference to it in the docs.

谢谢,

迈克

推荐答案

我收到了来自 Apple 开发者论坛的回复,并确认它没有考虑未保存的更改.

I've had a response from the Apple Developer Forums and it was confirmed that it doesn't take into account unsaved changes.

这篇关于NSDictionaryResultType 表达式不考虑新插入的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆