Search code examples
iosmultithreadingpfquery

Call a method after PFQuery returns data without using delegate


I recently started working on Parse SDK for iOS user signup/login. I followed tutorials provided on the Parse official documentation and everything works great as it is.

However, now I am trying to customize some of the queries. I want an action to take place AFTER I receive some data from another table. For that, I am using:

 [PFQuery findObjectsInBackgroundWithBlock^(BOOL return, NSError *error)].

When this block successfully executes, inside the block, I call my delegate's method, like so:

PFQuery *query = [PFQuery queryWithClassName:@"Requests"];
[query whereKey:kUserEmail equalTo:[dataDict objectForKey:kUserEmail]];
__block NSString *status = nil;
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        // The find succeeded.
        // Do something with the found objects
        for (PFObject *object in objects) {
            NSLog(@"%@", object.objectId);
            status = object[@"status"];

            if([del respondsToSelector:@selector(redirectToMyMethod:)])
            {
                [del redirectToMyMethod:status];
            }
        }
    } else {
        // Log details of the failure
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
}];

I wanted to know if there was any other way of achieving above mentioned functionality? Can't I somehow make my program wait until these returns have been fetched? So far, if I try to this without delegates, my program moves forward with incorrect return value and this PFQuery object's results are fetched later. I want to do something like this:

+ (BOOL) isUserInRequest{
PFQuery *query = [PFQuery queryWithClassName:@"Requests"];
[query whereKey:kUserEmail equalTo:[PFUser currentUser].username];

__block BOOL status = NO;

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if(!error)
    {
        if([objects count] > 0)
        {
            status = YES;
        }
    }
}];

return status;

}

But as you can see, I always get a return value of NO, and correct status value is updated later on. Can anybody help me with this?


Solution

  • The only way I could get this thing to work was to use call backs, as mentioned in the post in the comment. This wasn't the ideal solution that I was looking for, but I guess this will have to do. I am marking this question as closed.