简体   繁体   中英

ios swift parse: Having general issues with deep queries and their result data

I am still having problems understanding the correct way of handling deeper pointer structure in parse.

Example:

Card has pointer to CardSet

CardSet has pointers to Lesson and User

Lets say, I want to have all CardSets including

  • Lesson.name
  • Count of Cards for each CardSet

Can I query all this in just one query? And have the data available without any additional fetchIfNeededInBackgroundWithBlock queries?

I know that I can get the Lesson with

var query = PFQuery(className: "CardSet")
query.whereKey("user", equalTo: PFUser.currentUser())
query.includeKey("lesson")

But that gives me only the lesson object, I can not access any data (like the col "name") from this class unless I use fetchIfNeededInBackgroundWithBlock what takes another query and of course more time to load.

What can I do to have all queried data

including all pointers columns

in order to pin this data to the local datastore with

PFObject.pinAllInBackground(objects, block: nil)

And not to forget, how can I query the number of cards related to the CardSet?

First I would recommend subclassing in Parse, it makes relationships between tables (pointers etc) clearer. How to subclass the PFObjects you can explore this guide on parse

How to query the number of cards in CardSet

You need the CardSet from which you want the cards. (PFObject or subclassed PFObject). Then just do this:

var query = PFQuery(classname: "cards")
query.whereKey("CardSet", equalTo: yourCardSetObject)
//Synchronously
query.findObjects().count
//Asynchronously
query.findObjectsInBackgroundWithBlock({
      (objects, error) in
      if error != nil {
           println(error.localizedDescription)
           return
      }
      objects.count             
 })

How to get the name of the lesson

As I said, it's recommended to subclass the PFObjects because you need to cast the objects what isn't really funny to debug and is horrible code. I did it that way:

var query = PFQuery(className: "CardSet")
query.whereKey("user", equalTo: PFUser.currentUser())
query.includeKey("lesson")
query.findObjectsInBackgroundWithBlock({
     (objects, error) in
     if error != nil {
         println(error.localizedDescription)
         return
     }
     for object in objects as? [PFObject] {
         var lesson = object["lesson"] as PFObject
         println(lesson["name"])
     }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM