私はNSPredicateを初めて使用するので、誰かが私が正しい道を進んでいるかどうか教えてくれれば幸いです..
問題:(クラスはNSManagedObjectのサブクラスです)
私はこのフィールドを持つプレーヤークラスを持っています:
@property (nonatomic, retain) NSSet *hisNumbers; //it's a set of Numbers
Numbers クラスは次のようになります。
@property (nonatomic, retain) NSNumber * first;
@property (nonatomic, retain) NSNumber * second;
@property (nonatomic, retain) Season *forSeason; //it's one-to-one relationship with a Season Object
@property (nonatomic, retain) Player *owner; //one-to-one with Player
@property (nonatomic, retain) NSNumber * isPresent;
Season クラスには次のパラメータがあります。
@property (nonatomic, retain) NSSet *seasonGames; //set of Game
@property (nonatomic, retain) NSSet *numbers; //set of Numbers (inverse of forSeason)
私がやろうとしているのは、シーズンオブジェクトを取り、そのシーズンのすべてのプレーヤーを返すメソッドです...
これが私の試みです:
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Player" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"nameLast" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ANY SELF.hisNumbers.isPresent == %@) AND (ANY SELF.hisNumbers IN %@)",[NSNumber numberWithBool:YES],season.numbers];
しかし、それは機能しません..クエリの2つの部分は別々に機能するようですが、おそらくそれは単なる偶然です...
これで、フラグ「isPresent == YES」を持つ番号と、「season.numbers」のメンバーである別の番号を持つすべてのプレーヤーが返されます。代わりに、必要なのは、両方の基準に同時に一致する単一の「数字」を持っているプレーヤーです...
詳細が必要な場合はお知らせください..ありがとうございました