user3926564
user3926564

Reputation: 105

Cant get array PFQuery objects from parse code block

I dont know what the deal with parse is but for some reason it wont allow me to save the retrieved array into a mutable array I created. It works inside the parse code block but once outside, it displays null. Help please?

 PFQuery *query = [PFQuery queryWithClassName:@"comments"];
    [query whereKey:@"flirtID" equalTo:recipe.flirtID];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {


            comments = [[NSMutableArray alloc]initWithArray:objects];

            // Do something with the found objects
            for (PFObject *object in objects) {

            }
        } else {
            // Log details of the failure
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];
    NSLog(@"%@",[comments objectAtIndex:0]);

Upvotes: 0

Views: 749

Answers (1)

Daniel Brim
Daniel Brim

Reputation: 507

It's actually working as it should. You should read up on how blocks work.

Edit: Try reading Apple's Documentation

You're NSLogging 'comments' before comments actually gets set. How does that work? You see, query is running in the background, and it will actually take a bit of time. It's running asynchronously. But the code outside the block will run immediately.

While the code comes before, because it's an asynchronous block, it can and will be run whenever.

Try this:

comments = [[NSMutableArray alloc]initWithArray:objects];
NSLog(@"%@",[comments objectAtIndex:0]);

The important question is, what do you want to do after the query? Looks like you want to save comments, but then what? That will determine what you do next.

Upvotes: 1

Related Questions