Search code examples
iosswiftparse-platformpfquery

Parse.com Query to Get All Items with a Specific Pointer


I want to get all items from my Parse.com table called Sticker, from a particular shop. My Sticker table has a column called shopId. So the obvious solution is this:

//get all stickers from one shop of category dress
        var query = PFQuery(className:"Sticker")
        query.whereKey("shopId", equalTo: "QjSbyC6k5C")
        query.whereKey("category", equalTo: "DR")
        query.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]?, error: NSError?) -> Void in

            if error == nil {
                // The find succeeded.
                println("Successfully retrieved \(objects!.count) scores.")
                // Do something with the found objects
                if let objects = objects as? [PFObject] {
                    for object in objects {
                        println(object.objectId)
                    }
                }
            } else {
                // Log details of the failure
                println("Error: \(error!) \(error!.userInfo!)")
            }
        }

However that causes this error:

error: pointer field shopId needs a pointer value

I have seen a common solution for this seems to be to pass the query the actual object and not a string of the ID. Does this mean I have to first do a separate query to get the specific shop object, and then pass that to my query? Or is there a shorter way?

Here is my attempt to get the shop but it's causing this error:

Can only call -[PFObject init] on subclasses conforming to PFSubclassing

var query1 = PFQuery(className: "Shop")
        var shop1 = PFObject()
        query1.getObjectInBackgroundWithId("QjSbyC6k5C") {
            (shop: PFObject?, error: NSError?) -> Void in
            shop1 = shop!
        }

EDIT: So my solution was basically doing what the answer suggested. My code was this (Glamour is the name of the shop):

var shopQuery = PFQuery(className:"Shop")
        shopQuery.getObjectInBackgroundWithId("QjSbyC6k5C") {
            (glamour: PFObject?, error: NSError?) -> Void in
            if error == nil && glamour != nil {
                println(glamour)

                //get all stickers from one shop of category dress
                var query = PFQuery(className:"Sticker")
                query.whereKey("shopId", equalTo: glamour!)
                query.whereKey("category", equalTo: "DR")
                query.findObjectsInBackgroundWithBlock {
                    (objects: [AnyObject]?, error: NSError?) -> Void in

                    if error == nil {
                        // The find succeeded.
                        println("Successfully retrieved \(objects!.count) scores.")
                        // Do something with the found objects
                        if let objects = objects as? [PFObject] {
                            for object in objects {
                                println(object.objectId)
                            }
                        }
                    } else {
                        // Log details of the failure
                        println("Error: \(error!) \(error!.userInfo!)")
                    }
                }
            } else {
                println(error)
            }
        }

I will leave this question here and maybe someone will answer with a comment: Is there any way to get the shop and give it class scope so that we do not have to nest the second query inside the success of the first query? Would that be more elegant?


Solution

  • You need to pass PFObject. change your code with following

    PFObject *object = ...
        var query = PFQuery(className:"Sticker")
        query.whereKey("shopId", equalTo: "QjSbyC6k5C")
        query.whereKey("category", equalTo: object);