PFQuery FindObjectsInBackground Returns 0
Asked Answered
E

1

9

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.

Escudo answered 12/9, 2017 at 3:53 Comment(12)
Have you tried removing the cachePolicy line?Warrior
Yes, no change @WarriorEscudo
What is the simplest example that is not working? For example, try removing also the whereKey and orderBy. Try with a different class name. This will help us pinpoint the issue.Warrior
If you remove [query whereKey:@"Session" containedIn:follows];, does it work? What is follows?Slyke
@Slyke it does not. That is simply a constraint where I check if the Key session contains anything listed in the NSArray follows which is declared earlier. With or without constraints, it does not work.Escudo
How many objects are U going to find? How big is you Parse database?Downstage
I'm not sure what you are trying to do with the for loop but I'm guessing it doesn't do what you think it does. What you are doing in this case is you keep redefining the filter for "query" and only the filter that you specify on the last iteration of the for loop will count. You are not creating multiple whereKey with the loop, you will only end up with one whereKey.Superjacent
I think what would be helpful is if you printed "follows" before the for loop to see the contents. Can you add the print result to this thread? It would be helpful.Superjacent
Ok for @Superjacent and for EVERYONE ELSE WHO KEEPS ASKING THE SAME THING: the line [query whereKey has nothing to do with the issue. I've tried it with it in there, and I've tried it with removed, and it has no effect. The issue has to do with calling this, I suspect, from a ViewController and not a PFQueryTableViewController, but please...STOP ASKING ME ABOUT THAT ONE LINE AND IF REMOVING IT CHANGES ANYTHING! Look through comments and answers, it's been suggested countless times, so quit asking the same thing!!!!!!!Escudo
In your code, your comment says that the query completion block is never entered. How can you know then that 0 object are returned and there is no error?Liddell
Did you solve your problem by now? If so, please post the solution. It would not only be interesting for the people who wanted to help you, but also for others that may have the same problem.Liddell
I have not, unfortunatelyEscudo
B
1

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.

Blackstock answered 21/9, 2017 at 6:34 Comment(1)
The class name is fine, the filter is fine, and I HAVE tried it without the filter as I mentioned countless times already. THIS EXACT SAME CODE is used elsewhere in a PFQueryTableViewController without issues, and I suspect issue is with using it in a normal UIViewController, despite the fact that delegates are set.Escudo

© 2022 - 2024 — McMap. All rights reserved.