0

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?

2
  • You have quite alot going on here, My question would be, Would the user not have selected the particular shop beforehand (like in a picker or previous tableView or something)? Or are you manually passing the shop id all the time? In which case. Why? Commented Sep 11, 2015 at 8:21
  • @Devster101 The user will select the shop beforehand on a different ViewController (which I will implement later). I'm not sure how to take the shop object outside of the query. I'm learning Swift and Parse.com at the same time. Commented Sep 11, 2015 at 19:30

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I'm still working on the "..." part of your solution. Do you have example code for that? My code at the bottom of my question is my attempt.
Yes if you don't have PFObject then you have to get it first with first query then you can use second query.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.