user1555112
user1555112

Reputation: 1987

ios swift parse: Collect data from 3 classes

I have this structure:

I would like to select all card data including the name of the CardSet for the currentUser. I am quite lost here as I am new to parse and swift and don't know really how to get this done.

I checked the docs and understand I have to include

var query = PFQuery(className: "Card")
query.includeKey("cardset")

to get the cardset object. What I don't get is how to select the data only for the current user?

And at the end, how to get all data into an Array to use it for a table view and output the data to its table cell labels?

This is what I have so far:

var noteObjects: NSMutableArray! = NSMutableArray()
func fetchAllObjects(){

        var query = PFQuery(className: "Card")
        query.includeKey("cardset")
        query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

            if (error == nil){

                var temp: NSArray = objects as NSArray
                self.noteObjects = temp.mutableCopy() as NSMutableArray
                self.tableView.reloadData()

            }else{
                println(error.userInfo)
            }
        }
        println(self.noteObjects)
    }

Upvotes: 2

Views: 890

Answers (1)

danh
danh

Reputation: 62676

Your data model is sensible, but getting around it requires more complex queries. To get Cards whose CardSet user is the current user, you must embed a CardSet query within the Card query. The method that does this is whereKey:matchesQuery:

// this one finds CardSets whose user is currentUser
var innerQuery = PFQuery(className: "CardSet")
innerQuery.whereKey("user", equalTo:PFUser.currentUser())

// this one finds Cards whose CardSets match the preceding query
var query = PFQuery(className: "Card")
query.includeKey("cardset")
query.whereKey("cardset", matchesQuery:innerQuery);
query.findObjectsInBackgroundWithBlock //...

Upvotes: 2

Related Questions