2013-07-04 2 views
-1

아래와 같이 BOOL을 반환하는 새로운 메서드를 만들었습니다.BOOL 메서드가 블록 내부에서 YES를 반환하지 않습니다.

+(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; 
} 

I 가지는 문제가있어서 내의 블록 내의 값 YES를 리턴하는 방법이다. 메소드의 섹션을 참조하십시오. // 메소드에 반환 할 필요가없고 메소드에서 더 이상 실행하지 않습니다. 여기서 어떻게 YES를 리턴 할 수 있습니까? 내가 YES를 반환하면 호환되지 않는 포인터 유형 오류가 발생합니다.

이 메서드는 YES를 반환하면이 메서드를 호출하고 결과에 따라 무언가를 수행 할 수 있습니다. 예를 들어,이 메서드를 호출해야하며 true이면 아무 것도 수행하지 않으면 다른 작업을 수행해야합니다.

+0

나는 방법은 당신이 당신의 방법 안에 그 코드는 비동기 실행됩니다 생각 특히, 대신 BOOL 값을 반환하는 PARAMS 당신이 "성공/실패"블록을 사용하는 것이 좋습니다. – danypata

답변

3

나는 무엇을 묻고 있는지 확실하지 않으므로 다음과 같습니다. 값을 checkIfGameAlreadyExistsAgainst으로 되돌립니다.

블록을 구성 할 때 보통 복사본이 해당 환경에서 참조되는 값이됩니다. 블록을 으로 변경하려면 해당 환경의 변수를으로 수정하고 해당 변수를 __block으로 표시해야합니다. 코드에서이 같이 보일 것입니다 :

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

if (!blockStatus) 
{ 
    ... 
} 

중요 : findObjectsInBackgroundWithBlock를 호출하는 메소드의 이름은 블록이 호출이 이전에 반환 할 수 있습니다 의미 동 기적으로를 호출 할 수 없습니다 수 있음을 시사 블록이 실행됩니다. 이 경우 다른 방식으로 문제를 해결해야합니다. findObjectsInBackgroundWithBlock의 동기식 동등 호출 또는 checkIfGameAlreadyExistsAgainst을 수정하여 값을 직접 반환하지 않고 그 결과와 비동기 적으로 호출하는 블록을 허용하도록 할 수 있습니다.

HTH

+0

고마워, 그래, 그들은 비동기 그래서 괜찮을해야합니다. 블록 내부의 BOOL 값에 업데이트를 추가 한 다음이 값을 메서드의 끝에 오게됩니다. – StuartM

+1

@StuartM - 블록이 비동기 적으로 호출되면'findObjectsInBackgroundWithBlock'는 블록이 호출되기 전에 *를 반환 할 수 있으므로 * not * ok입니다. 그리고 블록이 호출되기 전에 다음 return 문은 BOOL 값을 반환합니다 그것을 바꾸는 것. 블록이 동 기적으로 호출되는 경우에만 올바르게 작동합니다. – CRD

+0

당신은 이것이 비동기 적이므로 블록이 실행되기 전에 반환이 반환됩니다. 이것은 내가 원하는 것이 아닙니다. 비동기가 아닌이 동기화를 만들기 위해 내가 할 수있는 일이 있는지 아십니까? findObjectsInBackgroundWithBlock 메서드를 소유하고 있지 않습니다. Parse.com에서 소유하고 있습니다. 이것을 교정하기 위해 내가 할 수있는 일이 있습니까? – StuartM

관련 문제