【发布时间】:2010-12-08 12:28:18
【问题描述】:
我有一些核心数据代码完全遵循 Apple 的 sample code(获取满足给定函数的属性值 示例)。我正在使用它来获取字段的最大值,因此我可以在插入该实体类型的下一个对象时递增它。
我根本无法让代码工作,直到我将我的商店类型从 NSXMLStoreType 切换到 NSSQLiteStoreType,然后突然之间一切似乎都正常了。然而,事实并非如此。我注意到它总是会返回相同的值,即使我插入了更高的对象。但是,在我退出并重新打开后(因此数据被持久化并读回),它会随着新的插入而更新。
然后我开始在每次插入后提交和保存。不过,在第一次“自动保存”之后,我收到以下错误(连续两次):
-[NSCFNumber count]:无法识别的选择器发送到实例 0x100506a20
当我执行一次获取请求时会发生这种情况(连续两次):
NSArray *objects = [context executeFetchRequest:request error:&error];
更新
我通过 Zombies 工具运行了我的代码,并且能够查看出现错误的对象。运行malloc 分配它的调用是:-[NSUserDefaults(NSUserDefaults) initWithUser:]。由于我没有设置任何自己的默认值,所以我不知道这可能是什么对象。
更新 2
我在所有代码中搜索了“发布”,并注释掉了静态分析器没有抱怨的每个 release 或 autorelease。我仍然得到错误。我什至在我的代码中注释掉最后一个release/autorelease,但仍然得到它。现在我相当确定我自己的代码没有过度发布。
更新 3
This post 似乎有同样的问题,但他的解决方案没有意义。他将结果类型从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;
【问题讨论】:
-
当你说它完全遵循 Apple 的代码时,你的意思是它实际上是逐字节相同的,还是说你可能在某个时候使用实例变量而不是局部变量?
-
你能加你的代码吗,你怎么把
request粘在一起? -
但这不是你的代码。
标签: objective-c cocoa macos core-data nsfetchrequest