我有一些核心数据代码完全遵循 Apple 的示例代码(获取满足给定函数示例的属性值)。我使用它来获取字段的最大值,因此我可以在插入该实体类型的下一个对象时增加它。
我根本无法让代码工作,直到我将我的 Store Type 从 切换NSXMLStoreType
到NSSQLiteStoreType
,然后突然之间一切似乎都正常了。然而,事实并非如此。我注意到它总是会返回相同的值,即使我插入了更高的对象。但是,在我退出并重新打开后(因此数据被持久化并读回),它会用新的插入更新。
所以我开始在每次插入后提交和保存。不过,在第一次“自动保存”之后,我收到以下错误(连续两次):
-[NSCFNumber count]:无法识别的选择器发送到实例 0x100506a20
当我执行一次获取请求时,会发生这种情况(连续两次):
NSArray *objects = [context executeFetchRequest:request error:&error];
更新
我通过 Zombies 工具运行了我的代码,并且能够查看出现错误的对象。malloc
运行分配它的调用是: -[NSUserDefaults(NSUserDefaults) initWithUser:]
. 由于我没有设置任何自己的默认值,我不知道这可能是什么对象。
更新 2
我在我的所有代码中搜索“发布”并注释掉静态分析器没有抱怨的每一个release
或。autorelease
我仍然得到错误。我什至在我的代码中注释掉最后一个release
/ autorelease
,但仍然得到它。现在我相当确定我自己的代码没有过度发布。
更新 3
这个帖子似乎有同样的问题,但他的解决方案没有意义。他将结果类型从 更改NSDictionaryResultType
为NSManagedObjectResultType
,这会产生不正确的结果。而不是返回单个值(max
我正在寻找的,它返回托管对象上下文中我的实体类的每个对象。
这是堆栈跟踪的最顶层(当我第一次让它在异常中中断时):
#0 0x7fff802e00da in objc_exception_throw
#1 0x7fff837d6110 in -[NSObject(NSObject) doesNotRecognizeSelector:]
#2 0x7fff8374e91f in ___forwarding___
#3 0x7fff8374aa68 in __forwarding_prep_0___
#4 0x7fff801ef636 in +[_NSPredicateUtilities max:]
#5 0x7fff800d4a22 in -[NSFunctionExpression expressionValueWithObject:context:]
#6 0x7fff865f2e21 in -[NSMappedObjectStore executeFetchRequest:withContext:]
#7 0x7fff865f2580 in -[NSMappedObjectStore executeRequest:withContext:]
我在网上其他地方的许多论坛上都看到过这个问题,但没有人提供可行的解决方案。应普遍要求,我在下面添加了自己的代码。稍微解释一下,我的实体名称是Box
,我试图获取的属性是“sortOrder”,一个Int 32
属性。
NSManagedObjectContext *context = [MyLibrary managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Box"
inManagedObjectContext:context]];
// Specify that the request should return dictionaries.
[request setResultType:NSDictionaryResultType];
// Create an expression for the key path.
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"sortOrder"];
// Create an expression to represent the function you want to apply
NSExpression *expression = [NSExpression expressionForFunction:@"max:"
arguments:[NSArray arrayWithObject:keyPathExpression]];
// Create an expression description using the minExpression and returning a date.
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
// The name is the key that will be used in the dictionary for the return value.
[expressionDescription setName:@"maxSort"];
[expressionDescription setExpression:expression];
[expressionDescription setExpressionResultType:NSInteger32AttributeType];
// Set the request's properties to fetch just the property represented by the expressions.
[request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];
// Execute the fetch.
NSError *error;
NSNumber *requestedValue = nil;
NSArray *objects = [context executeFetchRequest:request error:&error];
NSLog( @"objects: %@", objects );
if (objects != nil && [objects count] > 0) {
requestedValue = [[objects objectAtIndex:0] valueForKey:@"maxSort"];
} else {
[[NSApplication sharedApplication] presentError:error];
}
[expressionDescription release];
[request release];
NSLog( @"Max Sort Order: %@", requestedValue );
return requestedValue;