Zia
Zia

Reputation: 14722

Get 'many' objects in one-to-many relationship

I created my classes and inserted some data into them on Parse.com using a browser. Basically, I created classes A and B. A has a column of type B.

Now I want to display that data in my app. I am using the following code to get class A which is working fine.

    let query = PFQuery(className:"A")

    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
        if error == nil {
            // Do stuff
        } else {
            print("Error: \(error) \(error!.userInfo)")
        }
    }

However, I am not able to get class B data. I tried the following

query.includeKey("B")

This throws an error saying this is not a pointer relationship.

I also tried

let bObjects = aObject.objectForKey("B") as? [PFObject]

But the result is nil.

Question: How do I get 'many' objects in a one-to-many relationship in Parse?

Upvotes: 0

Views: 55

Answers (2)

Zia
Zia

Reputation: 14722

This is how I implemented the accepted answer:

let bObjectsRelation = self.relationForKey("B")
bObjectsRelation.query()?.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
    if error == nil {
       let bObjects = objects! as [PFObject]
    } else {
       print("Error: \(error) \(error!.userInfo)")
    }
}

Upvotes: 0

hhanesand
hhanesand

Reputation: 1000

If you're using relations, you should be using relationForKey.

Upvotes: 2

Related Questions