Parse.com querying User class (swift)
Asked Answered
E

4

7

I encounter some strange problem when I try to query all users from the "User" class It dos not find any Users

var query:PFQuery=PFQuery(className: "User");
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in
        if !(error != nil) {
            for(var i=0;i<objects.count;i++){
                var object=objects[i] as PFObject;
                var name = object.objectForKey("username") as String;
                println(name);

            }
        }
    }

I tried replacing

 var query:PFQuery=PFQuery(className: "User");

with

var query:PFQuery=PFQuery(className: "AnotherClass");

and everything worked like i should. I think it must be something special about the "User" class

Elli answered 2/10, 2014 at 20:8 Comment(3)
and what's the strange problem?Gehman
I think you should use "_User".Nodical
it works now after changing to "_User"!Elli
A
25

User isn't the appropriate name for the User table. You need to use the following:

var query : PFQuery = PFQuery(className: "_User")

A more appropriate method is:

var query : PFQuery = PFUser.query()
Artilleryman answered 2/10, 2014 at 20:53 Comment(2)
It seams better to use PFUser.query() rather than the string "_User" as it may change in the future, but the SDK will always return the correct query for user.Hugo
Thanks. I agree with you. I updated my answer. @HugoArtilleryman
V
6

You cannot query users like that. Instead you need:

var findUsers:PFQuery = PFUser.query();
findUsers.whereKey("username",  equalTo: searchText.text)

See Parse documentation:

https://parse.com/docs/ios_guide#users-querying/iOS

Velma answered 30/10, 2014 at 20:39 Comment(1)
Looks like Parse updated the link - parse.com/docs/ios/guide#users-queryingBuilding
Q
0

I've been stuck on this part of code too. I was able to query users and their data with this code:

    var usernames = [String]()
    var userImages = [Data]()

    var query : PFQuery = PFUser.query()!
                    // Interested in locations near user.
                    query.whereKey("location", nearGeoPoint: geopoint!)
                    // Limit what could be a lot of points.
                    query.limit = 10
                    do {
                        let queryObjects = try query.findObjects()
                        for object in queryObjects {
                            self.usernames.append(object["username"] as! String)
                            self.userImages.append(object["image"] as! Data)
                        }
                    } catch {
                        print("Error")
                    }
Quirita answered 22/3, 2018 at 7:55 Comment(0)
L
-1

try this ...

 var ObjectIDQuery = PFQuery (className: "User")
    ObjectIDQuery.findObjectsInBackgroundWithBlock({
        (objectsArray : [AnyObject]?, error: NSError?) -> Void in



        var objectIDs = objectsArray as! [PFObject]

        NSLog("\(objectIDs)")

        for i in 0...objectIDs.count-1{
            self.NameArray.append(objectIDs[i].valueForKey("username")as!  String)


            self.iDArry.append(objectIDs[i].valueForKey("objectId") as!  String)

            self.tableView.reloadData()
Lapin answered 25/7, 2015 at 17:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.