-1

以下に示すように、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 を返すメソッドを取得したら、このメソッドを呼び出して、結果に応じて何かを行うにはどうすればよいでしょうか。たとえば、このメソッドを呼び出す必要があり、それが真の場合は何か他のことを行い、そうでない場合は何もしません...

4

1 に答える 1

3

あなたが何を求めているのかわからないので、ここに推測があります: ブロックが に値を返すことを望みますcheckIfGameAlreadyExistsAgainst

ブロックが構築されると、通常、その環境から参照される値のコピーが作成されます。ブロックで環境内の変数を変更する場合は、その変数を でマークする必要があります__block。コードでは、これは次のようになります。

__block BOOL blockStatus = YES;
[currentUserIsUser1 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
 {
    ...
    blockStatus = NO;
 }
];

if (!blockStatus)
{
   ...
}

重要:呼び出しているメソッドの名前 は、ブロックが同期的findObjectsInBackgroundWithBlockに呼び出されない可能性があることを示しています。つまり、ブロックが実行される前に呼び出しが戻る可能性があります。この場合、別の方法で問題に取り組む必要があります。これには、値を直接返すのではなく、その結果と非同期的に呼び出すブロックを受け入れるように、同期的に同等のものを呼び出すか、変更する必要があります。findObjectsInBackgroundWithBlockcheckIfGameAlreadyExistsAgainst

HTH

于 2013-07-04T23:12:32.937 に答える