以下に示すように、BOOL を返す新しいメソッドを作成しました。
+(BOOL)checkIfGameAlreadyExistsAgainst:(PFUser *)opponentUser {
// Find all the games where the current user is user1 and the opponentUser is user2
PFQuery *currentUserIsUser1 = [PFQuery queryWithClassName:@"Game"];
[currentUserIsUser1 whereKey:kMESGameUser1 equalTo:[PFUser currentUser]];
[currentUserIsUser1 whereKey:kMESGameUser2 equalTo:opponentUser];
[currentUserIsUser1 whereKey:kMESGameIsActive equalTo:[NSNumber numberWithBool:YES]];
[currentUserIsUser1 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (objects) {
// We have games where the current user is user1
// NEED TO RETURN NO TO THIS METHOD AND NOT RUN FURTHER IN METHOD...
NSLog(@"Results returned for existing game where current user is User1. Results: %@",objects);
} else {
// If there are no objects from first query and no error we run the second query
if (!error) {
// Find all the games where the current user is user2 and the opponentUser is user1
PFQuery *currentUserIsUser2 = [PFQuery queryWithClassName:@"Game"];
[currentUserIsUser2 whereKey:kMESGameUser1 equalTo:opponentUser];
[currentUserIsUser2 whereKey:kMESGameUser2 equalTo:[PFUser currentUser]];
[currentUserIsUser2 whereKey:kMESGameIsActive equalTo:[NSNumber numberWithBool:YES]];
[currentUserIsUser2 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (objects) {
// We have games where the current user is user2
// NEED TO RETURN NO TO THIS METHOD AND NOT RUN FURTHER IN METHOD...
NSLog(@"Results returned for existing game where current user is User2. Results: %@",objects);
}
}];
}
}
}];
return NO;
}
私が抱えている問題は、メソッド内のブロック内で YES 値を返す方法です。// NEED TO RETURN TO THIS METHOD AND NOT RUN FURTHER IN METHOD... と書かれているメソッドのセクションを参照してください。ここで YES を返すにはどうすればよいですか。return YES を追加すると、互換性のないポインター型エラーが発生します。
これに加えて、YES を返すメソッドを取得したら、このメソッドを呼び出して、結果に応じて何かを行うにはどうすればよいでしょうか。たとえば、このメソッドを呼び出す必要があり、それが真の場合は何か他のことを行い、そうでない場合は何もしません...