【问题标题】:BOOL method not returning YES inside of blockBOOL 方法未在块内返回 YES
【发布时间】:2013-07-02 23:09:39
【问题描述】:

我创建了一个返回 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 NO TO this METHOD AND NOT RUNFTHER IN METHOD... 我怎样才能在这里返回 YES。如果我添加 return YES,我会得到一个不兼容的指针类型错误。

除此之外,一旦我的方法返回 YES,我该如何调用该方法并根据结果执行某些操作。 例如我需要调用这个方法,如果它是真的然后做别的事情,如果不是什么都不做......

【问题讨论】:

  • 我建议您使用“成功/失败”块作为方法参数,而不是返回 BOOL 值,尤其是当您认为方法中的代码将异步执行时。

标签: ios objective-c ios5 boolean objective-c-blocks


【解决方案1】:

我不确定你在问什么,所以这里有一个猜测:你希望你的块返回一个值到checkIfGameAlreadyExistsAgainst

在构造块时,它通常会复制从其环境中引用的任何值。如果您希望您的块在其环境中修改一个变量,您必须用__block 标记该变量。在您的代码中,这看起来像:

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

if (!blockStatus)
{
   ...
}

重要提示:您正在调用的方法的名称findObjectsInBackgroundWithBlock,表明该块可能不会被同步调用,这意味着调用可能会在调用之前返回块被执行。如果是这种情况,您需要以不同的方式解决问题;这可能涉及调用findObjectsInBackgroundWithBlock 的同步等效项或修改checkIfGameAlreadyExistsAgainst 以便它接受一个块,它会以其结果异步调用而不是直接返回值。

HTH

【讨论】:

  • 谢谢,是的,它们是异步的,所以应该没问题。我已将更新添加到块内的 BOOL 值,然后在方法结束时返回该值,该方法可以正常工作。
  • @StuartM - 如果块被异步调用,那么它 ok,因为findObjectsInBackgroundWithBlock 可能会返回before块被调用,并且然后后面的 return 语句将在块有机会更改它之前返回 BOOL 值。只有在同步调用块时才能正常工作。
  • 你是对的,这是异步的,因此在块运行之前返回返回,这不是我想要的。你知道我可以做些什么来使这种同步而不是异步吗?我不拥有 findObjectsInBackgroundWithBlock 方法,它们归 Parse.com 所有。我有什么办法可以用我自己的方法来解决这个问题吗?
  • @StuartM - 我从未使用过这个包,但快速浏览一下docs 表明findObjectsInBackgroundWithBlock:findObjects: 的异步版本。
  • 然后我是否需要在后台线程上触发 findObjects 以确保它不会锁定 UI,但反过来会使其异步,我不确定吗?但是是的,你是正确的 findObjects/countObjects 是正常的同步选项。
猜你喜欢
  • 2015-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-08
  • 2013-02-28
  • 2016-05-27
  • 2021-12-20
  • 2016-02-01
相关资源
最近更新 更多