1

NSPredicate を使用して最初の選択から一部のオブジェクトを除外し、NSExpressionを使用し管理対象オブジェクトの日付属性の最大値のみを取得するフェッチ リクエストを実行しています。Apple がその例 ( Apple Developer ) で示したものとまったく同じコードを使用していますが、この式は配列として最大値が必要な属性を何らかの形で考慮しているように見えるため、フェッチを実行するたびに、アプリ「認識されないセレクター」例外でクラッシュします ( countメッセージを__NSDateに送信しようとしています)実例)。属性名を文字列属性に変更すると、 __NSCFStringインスタンスまたは同等のものに対して同じ例外が発生します)。これは、オブジェクトを見つけて正しい属性を使用していることを意味しますが、どういうわけか式が正しく適用されていません。

私のコードは次のとおりです。

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"BridgeOpening" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
request.predicate = [NSPredicate predicateWithFormat:@"lastUpdated != nil && bridge.location == %@", location];

// Specify that the request should return dictionaries.
[request setResultType:NSDictionaryResultType];

// Create an expression for the key path.
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"lastUpdated"];

// Create an expression to represent the maximum value at the key path 'creationDate'
NSExpression *maxExpression = [NSExpression expressionForFunction:@"max:" arguments:[NSArray arrayWithObject:keyPathExpression]];

// Create an expression description using the maxExpression 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:@"maxDate"];
[expressionDescription setExpression:maxExpression];
[expressionDescription setExpressionResultType:NSDateAttributeType];

// Set the request's properties to fetch just the property represented by the expressions.
[request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];

// Execute the fetch.
NSError *error = nil;
NSLog(@"executing fetch");
NSArray *objects = [managedObjectContext executeFetchRequest:request error:&error];
NSLog(@"fetch finished");
if (objects == nil) {
    // Handle the error.
}
else {
    if ([objects count] > 0) {
        NSLog(@"Minimum date: %@", [[objects objectAtIndex:0] valueForKey:@"maxDate"]);
        lastUpdate = [[objects objectAtIndex:0] valueForKey:@"maxDate"];
    }
}

「fetch finished」ログに到達することはないため、例外は間違いなくフェッチ リクエストの実行によって発生します。

式が何をしていてもNSArrayを期待しているように見える理由、または私が間違っていることを知っている人はいますか? (およびNSManagedObjectResultType )なしでフェッチ リクエストを実行しても問題はなく、が適用される前に結果が存在しないような述語であれば問題はないことを付け加えておきます。

これはおそらくこの投稿で発生した問題と同じだと思いますが、受け入れられた解決策はなく、元の投稿者が提供した答えは受け入れられません ( NSDictionaryResultTypeの代わりにNSManagedObjectResultTypeを使用すると、が完全にバイパスされ、ほぼ確実に否定されると思います)を使用するメモリの利点。

EDIT:これを最初に述べるのを忘れましたが、フェッチリクエストで述語 を使用するかどうかに関係なく、同じ問題があります。

4

0 に答える 0