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

查看:152
本文介绍了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.

感谢,

Mike

推荐答案

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天全站免登陆