Appleのサンプルコードに正確に従ったコアデータコードがいくつかあります(特定の関数の例を満たす属性値の取得)。フィールドの最大値を取得するために使用しているので、そのエンティティタイプの次のオブジェクトを挿入するときにインクリメントできます。
NSXMLStoreType
ストアタイプをからに切り替えるまで、コードをまったく機能させることができませんでしたNSSQLiteStoreType
。その後、突然すべてが機能しているように見えました。ただし、そうではありません。より高いオブジェクトを挿入した場合でも、常に同じ値が返されることに気付きました。しかし、終了して再度開いた後(したがって、データは永続化されて読み戻された後)、新しい挿入で更新されます。
それで、私は各挿入の後にコミットして保存し始めました。ただし、最初の「自動保存」の後、以下のエラーが発生します(2回続けて)。
-[NSCFNumberカウント]:認識されないセレクターがインスタンス0x100506a20に送信されました
これは、フェッチ要求を1回実行すると(2回続けて)発生します。
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;