user6032625
user6032625

Reputation: 197

How to use a where key array in a string column?

I have an array of names, and a column which is of type string, and I am trying to get the information. When I use a for loop for each value in the where key, I get A LOT of the same info, so say there are 5 names, and I am trying to pick up rate (an Integer) I will get like 100 values instead of just 5.

var name = ["Dog", "Cat", "Monkey"]
let query = PFQuery(className: "Animals")
    query.whereKey(not sure what to put since it is an array)
    query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) in
        if(error == nil){
            for object in objects!{


                        if let rating = object["rate"] as? Int{
                            self.rater.append(rating)
                            print("rating \(self.rater)") //There are like 75-100 values
                        }
            }
        }else{
            print(error)
            }
        }

Upvotes: 0

Views: 71

Answers (2)

Nikhil Modi
Nikhil Modi

Reputation: 187

Try this, it will surely works.

var names = ["Dog", "Cat", "Monkey"]
    let query = PFQuery(className: "Animals")
        query. whereKey("name", containedIn: names)
        query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) in
            if(error == nil){
                for object in objects!{


                            if let rating = object["rate"] as? Int{
                                self.rater.append(rating)
                                print("rating \(self.rater)") //There are like 75-100 values
                            }
                }
            }else{
                print(error)
                }
            }

Upvotes: 0

Marcos Crispino
Marcos Crispino

Reputation: 8218

I don't know much about Parse, but there is this method -whereKey:containedIn: which takes an array. Won't that work for you? (sorry I didn't find the Swift documentation)

Documentation says:

-whereKey:containedIn:

Add a constraint to the query that requires a particular key’s object to be contained in the provided array.

Declaration OBJECTIVE-C

- (nonnull instancetype)whereKey:(nonnull NSString *)key
                 containedIn:(nonnull NSArray *)array;

Parameters:

-key The key to be constrained.

-array The possible values for the key’s object.

Upvotes: 1

Related Questions