Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PFQuery FindObjectsInBackground Returns 0

In my UIViewController I am trying to query my parse server, but I keep getting a return of 0 for it, though I know 100% that this class does have objects in it. Any thoughts?

 PFQuery *query = [PFQuery queryWithClassName:@"General"];

 int i;
 for (i = 0; i < [follows count]; i++) {
        [query whereKey:@"Session" containedIn:follows];
 }
 query.cachePolicy = kPFCachePolicyCacheThenNetwork;

 [query orderByDescending:@"createdAt"];
 [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
 // it never gets here...
 NSLog(@"OBJECTS%@", objects);
 if (!error) {
     NSLog(@"Successfully retrieved %lu objects.", (unsigned long)objects.count);
     for (PFObject *object in objects) {
         NSLog(@"%@", object.objectId);
     }

     // [self gotoMain];
 } else {
       NSLog(@"Error: %@ %@", error, [error userInfo]);
   }
 }];

It tells me there is no error that it successfully retrieved 0 objects in my console.

like image 511
user717452 Avatar asked Sep 12 '17 03:09

user717452


1 Answers

As other already suggested, I would first do the simplest query:

PFQuery *query = [PFQuery queryWithClassName:@"General"];
 [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
 if (!error) {
     NSLog(@"Successfully retrieved %lu objects.", (unsigned long)objects.count);
 } else {
       NSLog(@"Error: %@ %@", error, [error userInfo]);
   }
 }];  

If it is executed without error, returns 0 objects, and the dashboard shows that there are objects that should be returned, the class name must be wrong. So please double check the class name, e.g. the spelling.

If objects are returned, your filter must be wrong. Your for is wrong for two reasons anywhere:
1) The for loop is executed follows.count - times, but it executes always the same instruction, since index is not used. I guess that you wanted to write (but this is also wrong)

 for (i = 0; i < [follows count]; i++) {
        [query whereKey:@"Session" containedIn:follows[i]];
 }  

2) This is wrong because you can only have a single filter whereKey:containedIn:. As has been mentioned by DevKyle, this single filter is overwritten follows.count-1 - times, and only the last filter is used.
I guess you wanted to have something like a logical OR of the individual filters. If so, you had to flatten you array, i.e. make a single array NSArray *flattenedFollows of all the elements in follows[i], see here and set then a single filter

 [query whereKey:@"Session" containedIn: flattenedFollows];  

EDIT:
One last idea: If your query is correct (besides of the for loop) and no object is returned anyway, it might be that you don't have the right to access them. So, please, check that the ACL field of these records has the correct access rights.

like image 191
Reinhard Männer Avatar answered Nov 19 '22 06:11

Reinhard Männer