简体   繁体   中英

Return bool inside pfcloud function

I'm creating a method that checks if the user has any friends who are already registered to the app. However, when I'm trying to return from within this block, i get a compiler error saying: Incompatible block pointer types sending 'BOOL (^)(__strong id, NSError *__strong)' to parameter of type 'PFIdResultBlock' (aka 'void (^)(__strong id, NSError *__strong)')

This is when I run the code below:

 -(BOOL)hasFriend:(NSArray*)phoneNums{
    [PFCloud callFunctionInBackground:@"checkUsers"
                                   withParameters:@{@"array": phoneNums}
                                            block:^(id success, NSError *error) {
                                              if(success){
                                                return YES;
                                              }
                                              else{
                                                return NO;
                                                  }];
    }

I've also tried this:

-(BOOL)hasFriend:(NSArray*)phoneNums{
  __block bool hasFriend = nil;

  [PFCloud callFunctionInBackground:@"checkUsers"
                   withParameters:@{@"array": phoneNums}
                            block:^(id success, NSError *error) {
                              if(success){
                                hasFriend = YES;
                                NSLog(@"found a florin user!");
                              }
                              else{
                                hasFriend = NO;
                                }
                              }
                            }];

  NSParameterAssert(hasFriend);
  return hasFriend;
}

However, this never passes the NSParameterAssert .

The block isn't executed until after the function returns so you can't possibly return a value from the function... You will need to use a callback block (another one) in your function:

-(void) hasFriend:(NSArray*)phoneNums withCallback:(void(^)(BOOL hasFriend))callback {

    [PFCloud callFunctionInBackground:@"checkUsers"
                       withParameters:@{@"array": phoneNums}
                                block:^(id success, NSError *error) {

                                    if (success)
                                        callback(YES);
                                    else
                                        callback(NO);

                                }];

}

And to use it, you'll need to supply a callback like this:

[myObj hasFriend:nums withCallback:^(BOOL hasFriend) {

    if (hasFriend)
        NSLog(@"Found friend!");
    else
        NSLog(@"Friend not found...");

}];

The PFIdResultBlock has no return type ( **void** (^)(__strong id, NSError *__strong ). "callFunctionInBackground" is asynchronous operation. It means at the line of return hasFriend you actually cannot receive the result you need.

Just push the completion block as a method parameter like this

- (void)checkHasFriendWithNums:(NSArray *)phoneNums
{
  [PFCloud callFunctionInBackground:@"checkUsers"
                   withParameters:@{@"array": phoneNums}
                            block:^(id success, NSError *error) {
                              if(success){
                                hasFriend = YES;
                                NSLog(@"found a florin user!");
                              }
                              else{
                                hasFriend = NO;
                                }
                              if(completion) // perform the block
                                completion(hasFriend);
                              }
                            }];
}

- (void)someMethodWithPhoneNums:(NSArray *)phoneNums
{
  [self checkHasFriendWithPhoneNums:phoneNums completionBlock:^(BOOL hasFriend){
       if (hasFriend)
       {
          // do something
       }
  }];
}

If you don't like blocks - use methods instead.

- (void)checkHasFriendWithNums:(NSArray *)phoneNums completionBlock:(void(^)(BOOL hasFriends))completion
{
  [PFCloud callFunctionInBackground:@"checkUsers"
                   withParameters:@{@"array": phoneNums}
                            block:^(id success, NSError *error) {
                              if(success)
                                [self hasFriends];
                              else
                                [self hasNoFriends];
                              }
                            }];
}

- (void)hasFriends
{
  // do something
}

- (void)hasNoFriends
{
  // do something
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM