在 Swift 中的 PFQuery Table 视图控制器中查询关系数据

Querying for Relation Data in a PFQuery Table View Controller in Swift

我试图通过访问 Parse 中的 "friends" 列来检索当前用户的好友。我设置朋友的方式是让用户 select 用户位于另一个视图中,然后将其添加到 Parse 中其用户行中名为 "friends" 的关系中。我可以让用户在 Parse 中另存为关系,但我无法弄清楚如何让这些相关用户真正显示在朋友 Table 视图中。这是我当前的代码:

 override func queryForTable() -> PFQuery {

    let currentuser = PFUser.currentUser()

    // Start the query object
    let query = PFQuery(className: "_User")


    // Add a where clause if there is a search criteria
    if FriendSearch.text != "" {
        query.whereKey("username", containsString: FriendSearch.text)
    }

    // Order the results
    query.orderByDescending("score")

    // Return the query object
    return query
}

如何让当前用户的相关用户出现在 PFTable 视图控制器中?谢谢

您没有创建新查询(特别是使用用户的私人 class 名称)。相反,您从用户那里获得关系并向关系询问其查询。一旦你有了它,你就可以向它添加额外的参数并 return 它。

我弄清楚我做错了什么。我需要为当前用户的关系创建一个值,然后进行查询。

// Define the query that will provide the data for the table view
override func queryForTable() -> PFQuery {

    **let currentuser = PFUser.currentUser()?.relationForKey("friends")

    // Start the query object
    let query = currentuser?.query()**


    // Add a where clause if there is a search criteria
    if FriendSearch.text != "" {

        query!.whereKey("username", containsString: FriendSearch.text)
    }

    // Order the results
    query!.orderByDescending("score")

    // Return the qwuery object
    return query!
}