ios swift parse:从3个类中收集数据

时间:2015-03-03 16:09:04

标签: ios swift parse-platform

我有这个结构:

  • 用户
  • CardSet,指针“user”指向User.objectId,指向col“name”
  • 卡片上带有指针“cardset”到CardSet.objectId和一个col“name”

我想选择所有卡数据,包括当前用户的CardSet名称。我很失落,因为我是新手解析和快速,并不知道如何完成这项工作。

我查看了文档,并了解我必须包含

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

获取卡集对象。 我没有得到的是如何仅为当前用户选择数据?

最后,如何将所有数据导入数组以将其用于表格视图并将数据输出到其表格单元格标签?

这是我到目前为止所做的:

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)
    }

1 个答案:

答案 0 :(得分:2)

您的数据模型是明智的,但绕过它需要更复杂的查询。要获取Cards用户是当前用户的CardSet,您必须在Card查询中嵌入CardSet查询。执行此操作的方法是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 //...