Cliffordwh
Cliffordwh

Reputation: 1430

Parse relational type query - swift 3

I've got 2 classes

Reports - objectID, Title, Date & relationItem ( Relation type column linked up to Items) 
Items - ObjectID, Title, Date etc

I want to query all the Items that are equal to a objectID in reports. Users create reports then add items to them. These items are found in the Items table.

I've looked at the https://parseplatform.github.io/docs/ios/guide/#relations but don't see anything for swift3.

I've tried a few things with little success. This snipplet below i did find, but not sure how to apply it to my classes.

 var relation = currentUser.relationForKey("product")
 relation.query()?.findObjectsInBackgroundWithBlock({

Would love somebody to direct me into the right direction! Thanks

Tried this code below too!

var query = PFQuery(className:"Items")
query.whereKey("relationItem ", equalTo: PFObject(withoutDataWithClassName:"Reports", objectId:"MZmMHtobwQ"))

Upvotes: 0

Views: 415

Answers (2)

Cliffordwh
Cliffordwh

Reputation: 1430

Ok so i had to change the table slightly to get this to work to prevent a query within a query.

I've added a relation Type to the Items table instead of the Reports Table

Then i managed to retrieve all the Items based of that report ObjectId like this:

 let query = PFQuery(className:"Items")
     query.whereKey("reportRelation", equalTo: PFObject(withoutDataWithClassName:"Reports", objectId:"3lWMYwWNEj"))

This then worked. Note that reportRelation is the Relational Type Column. Thanks

Upvotes: 2

Leandro P
Leandro P

Reputation: 213

When you’re thinking about one-to-many relationships and whether to implement Pointers or Arrays, there are several factors to consider. First, how many objects are involved in this relationship? If the “many” side of the relationship could contain a very large number (greater than 100 or so) of objects, then you have to use Pointers. If the number of objects is small (fewer than 100 or so), then Arrays may be more convenient, especially if you typically need to get all of the related objects (the “many” in the “one-to-many relationship”) at the same time as the parent object.

http://parseplatform.github.io/docs/ios/guide/#relations

If you are working with one to many relation, use pointer or array. See the guide for examples and more explanation.

Upvotes: 0

Related Questions