0

Just started building an iOS application using Parse. I meticulously followed the Getting Started Guide and as expected, can create and save Parse objects as well as login Users. Unfortunately, when I create a PFQuery Object and try to retrieve data from parse, the query method itself executes without an error, but returns no data and in my case ignores the for-in statement in the code block. I find this particularly odd because I literally copy pasted this code from the Parse documentation.

Could someone kindly explain to me why this isn't working/provide me with a better solution? Would be eternally grateful!!

Here's my code:

(void)viewDidLoad {
    [super viewDidLoad];

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

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {

            NSLog(@"Test 1 retrieved %d Users.", objects.count);

            for (PFObject *object in objects) {

                NSLog(@"Test 2 Retrived %d Users.", objects.count);
                [self.array addObject: object];
            }
        } else {

            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];

    NSLog(@"%@", self.array);

}

Here's the result of the NSLog's:

2016-01-25 10:11:56.708 FSJ[4507:405721] (null)
2016-01-25 10:11:56.790 FSJ[4507:405721] Test 1 retrieved 0 Users.

Note: 1) From what I've read people seem to think this may have something to do with threading, but I tried using the findObject Method as well, which though not recommended uses the main thread, and received the same result. 2) Also, thought it might have to do with access permissions, but I made sure that in my data browser all my Read, Write, and Add permissions are checked off and are public.

2
  • findObjectsInBackgroundWithBlock: is async, that's normal log "(null)" before "Test 1" log. The issue is on the fact that you have "0" users retrieved. It's just a clarification since that doesn't seems clear for you. Commented Jan 25, 2016 at 15:48
  • as @Rusell answered, you need to use a user query userQuery = PFuser.query() , in case Parse decides to make any changes to their classes, you will be unaffected using this method, where as if you query the user class the way you currently are doing it, you will have to re-write your code Commented Jan 25, 2016 at 20:25

3 Answers 3

1

You need to use [PFQuery queryWithClassName:@"_User"]; instead of [PFQuery queryWithClassName:@"User"];

Even better, you should use the following when querying against the PFUser class

PFQuery *query = [PFUser query];
Sign up to request clarification or add additional context in comments.

Comments

0
[super viewDidLoad];

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

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {

        NSLog(@"Test 1 retrieved %d Users.", objects.count);

        for (PFObject *object in objects) {

            NSLog(@"Test 2 Retrived %d Users.", objects.count);
            [self.array addObject: object];

        }

        //After the objects have been added to the array,
        //Now log the array
        NSLog(@"%@", self.array);

    } else {

        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
}];

//This line will be executed before [query findObjectsInBackgroundWithBlock]
//Because it's run async. The array would not be populated yet at this point
//NSLog(@"%@", self.array);

2 Comments

Much appreciated Greg!! That seems to help.
@eak not sure that I agree with this as the marked answer. The actual reason that 0 users are returned as a result is that the user class was being incorrectly queried against with User instead of _User. Welcome to StackOverflow and always try to upvote useful posts in addition to marking as answer :)
0

You actually need to use a particular type of Query, a User query PFUser.query()

here is an example in swift

func fetchUsers() {
    let userQuery: PFQuery = PFUser.query()!

    userQuery.orderByAscending("username")
    userQuery.whereKey("username", notEqualTo: (currentUser?.username)!)
    userQuery.findObjectsInBackgroundWithBlock({
        (users, error) -> Void in

        var URLs = users!

        if error == nil {
            if URLs.count >= 1 {
                for i in 0...users!.count-1 {
         self.urlsArray.append(URLs[i].valueForKey("indexPathRow") as! Int)
                }
            }

            self.usersArray = users as! [PFUser]
        } else {
            print(error)
        }
    })
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.