简体   繁体   中英

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]);

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.

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