0

Scenario = I am trying to fetch data from parse and store that data into an array I can use anywhere in my view controller class.

Problem = When I run this below, Log 2 returns "(null)". The contents of "objects" seems like it doesn't want to leave the findObjects block. And it needs to otherwise the fetch was pointless.

PFQuery *messageQuery = [PFQuery queryWithClassName:@"Message"];
[messageQuery whereKey:@"receiverID" equalTo:[PFUser currentUser][@"userID"]];
[messageQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

    messageArray = objects;

    NSLog(@"Log 1 = %@", messageArray);

}];

NSLog(@"Log 2 = %@", messageArray);

Question = Why is this happening? Or not happening?

I have tried putting the NSLog and the "messageArray = objects" inside of a dispatch_async and it still returns "(null)". What gives?

1 Answer 1

0

Assuming findObjectsInBackgroundWithBlock() is performing it's work in another thread/queue than the current one, the code flow is as follows:

1. [messageQuery findObjectsInBackgroundWithBlock:...];
2. NSLog(@"Log 2 = %@", messageArray);
3. ^(NSArray *objects, NSError *error) {
     messageArray = objects;
     NSLog(@"Log 1 = %@", messageArray);
}

So your second log is performed before messageArray = objects; gets called. [messageQuery findObjectsInBackgroundWithBlock:...]; returns immediately and calls your completionBlock sometimes in the future. If you want to update some controls in your ViewController when messageArray gets set, you should put that code or an appropiate method call in your completionBlock, e.g.:

PFQuery *messageQuery = [PFQuery queryWithClassName:@"Message"];
[messageQuery whereKey:@"receiverID" equalTo:[PFUser currentUser][@"userID"]];
__weak tyepof(self) myself = self;
[messageQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

    messageArray = objects;

    NSLog(@"Log 1 = %@", messageArray);

    [myself updateWithMessages:messageArray];

}];

__weak tyepof(self) myself = self; is required to avoid a retain cycle in your completionBlock.

Sign up to request clarification or add additional context in comments.

3 Comments

I don't understand. The first bit of code you put is laid out 1, 2, 3. 3 is a block of code, where does that block of code go? (what is it a block in relation to?)
I appreciate your response. But I regret to inform you the code you supplied to me turns my brain into a pretzel. I have no idea what any of that is supposed to mean or do. The first bit of code you laid out in 1, 2 and 3. 3 is a block - To what is that block supposed to be in relation to? If it's a completion block can you please supply the rest of the method/function? "__weak tyepof(self) myself = self;" - I have never seen anything like that, ever (turns brains into a flushed toilet). This boggles the mind.
I have expanded my question in another post, can you check that out and tell me what you think. Sorry. stackoverflow.com/questions/24193085/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.