The last NSLog(@"The dictionary is %@", self.scoreDictionary) statement NSLog(@"The dictionary is %@", self.scoreDictionary) does not actually execute after the block completes. It is executed after the findObjectsInBackgroundWithBlock method findObjectsInBackgroundWithBlock . findObjectsInBackgroundWithBlock supposedly starts something in a separate thread, and your block may actually not run at all until some time after this last NSLog statement. Graphically, something like this could happen:
Thread 1 -------- retriveDataFromParse called invoke findObjectsInBackgroundWithBlock findObjectsInBackgroundWithBlock queues up work on another thread findObjectsInBackgroundWithBlock returns immediately | NSLog statement - self.scoreDictionary not yet updated | retriveDataFromParse returns | . V . Thread 2, starting X milliseconds later . -------- . findObjectsInBackgroundWithBlock does some work . your block is called . for-loop in your block . Now self.scoreDictionary has some data . NSLog statement inside your block
You should probably think about what you want to do with your scoreDictionary data after you receive it ? For example, do you want to update the user interface, call another method, etc.? You will want to do this inside your block, after which you know that the data has been successfully retrieved. For example, if you had a table view that you wanted to reload, you could do this:
for (PFObject *object in objects){ .... } dispatch_async(dispatch_get_main_queue(), ^{ [self updateMyUserInterfaceOrSomething]; });
Pay attention to dispatch_async - if the work that you need to do after updating your data is related to changing the user interface, you want this to be done in the main thread.
Mike mertsock
source share