Search code examples
iosxcodeios7parse-platformxcode6

iOS App freezes during Parse Asynchronous Query


I'm querying Parse for some objects using a background asynchronous call. However, while the query is happening, the app freezes. I can't interact with it. Once the query is done and the objects are loaded though, the app catches up. It's my understanding that the app should be responsive during the query because it's a background asynchronous task.

For example, when the app loads the query begins -> if I press back on the nav bar during the query nothing happens -> the query finishes and my objects appear -> the app pops up to the root controller in response to the earlier touches

The app is not frozen after the query. It responds normally. I'm not sure if the problem is my placement of the query, that the phone just gets slow when it parses, or what. Any help would be appreciated!!

Relevant code:

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    //Loads the users to be rated
    [self loadNewUsersAndAd];
}

-(void)loadNewUsersAndAd{
    self.usersQuery = [PFQuery queryWithClassName:@"UserObjects"];
    [self.usersQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        self.queryingForUsers = YES;
        if (error) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry!" message:@"It looks like we couldn't update your current games! Make sure you're connected to the internet." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alertView show];
            self.queryingForUsers = NO;
        }
        else 
        {
            //The query was successful
            self.usersArray = objects;
           [self loadNewImages];
           self.queryingForUsers = NO;

    }

}];



}

Solution

  • You shouldn't update UI elements from a background thread. Ensure that your loadNewImages is dispatched on the main queue -

     self.usersArray = objects;
     dispatch_async(dispatch_get_main_queue(), ^{
         [self loadNewImages];
         self.queryingForUsers = NO;
     });