Search code examples
iosasynchronousparse-platformsynchronouspfquery

PFQuery synchronous call works, asynchronous call fails


I am new to using parse and experience some problems querying the data I have added in my parse class. My problem is that I can get the synchronous call ([query findObjects]) working, the asynchronous call ([queryInBackground...]) however fails.

Here are the two code snippets:

-(void)getAllDataFromParse{
    //simple query works
    PFQuery *query = [PFQuery queryWithClassName:@"wordsDB"];
    [query setLimit: 1000];
    NSArray *objects = [query findObjects];
    }
   //background query not working
   PFQuery *queryInBackground = [PFQuery queryWithClassName:@"wordsDB"];
   [queryInBackground findObjectsInBackgroundWithBlock:^(NSArray *objects2, NSError *error) {
       if (!error) {
           //query succeeds, do something
           }
       } else {
          // Log details of the failure
          NSLog(@"Error: %@ %@", error, [error userInfo]);
       }
  }];
}

This method is called in my mainViewController, the call is at the end of the viewDidLoad function

[self performSelector:@selector(getAllDataFromParse)];

In debugging, the program reaches [queryInBackground findObjectsInBackgroundWithBlock.... ] but when executing it, it jumps straight to the end of the method.

There is no error message I can see. Can anyone tell me what is going wrong with my asynchronous call?

I have tried running it on emulator and real device.


Solution

  • This is an asynchronous call meaning that it will continue running in the background. It going to the end of the method is perfectly normal.

    [queryInBackground findObjectsInBackgroundWithBlock:^(NSArray *objects2, NSError *error) {
           if (!error) {
               //query succeeds, do something
               }
           } else {
              // Log details of the failure
              NSLog(@"Error: %@ %@", error, [error userInfo]);
           }
      }];
    

    This may also help.