ios - swift + Parse.com : how to fetch user relation using a join table

标签 ios swift parse-platform pfuser

我有一个像 instagram 这样的应用程序,我必须在其中建立用户之间的关系。 为此,我决定使用连接表方法。 它只是一个名为“Activity”的表,我在其中创建了行“fromUser”、“toUser”和事件类型“type”的一行,在本例中设置为“followingAction”。

这里是我如何设置表格的:

    var activity = PFObject(className:"Activity")

        let currentUser = PFUser.currentUser()

        let follow : String = "followingAction"

        activity.setObject(currentUser, forKey: "fromUser")
        activity.setObject(user, forKey: "toUser") // user is a another PFUser in my app

        activity.setObject(follow, forKey: "type")

        activity.saveEventually()

好的,现在我想获取我当前关注的所有用户 (currentUser) 并将它们显示在 tableView 中

按照文档 https://parse.com/docs/relations_guide#manytomany-jointables

我做了这个,但它只给我一个只包含用户 objectId 的用户数组,我没有在常规用户表中设置的电子邮件、名称:

       func loadUser () {

    followingUserList.removeAllObjects()

    let findUserObjectId =       PFQuery(className: "Activity")
    findUserObjectId.whereKey("fromUser", equalTo: userPassed)
    findUserObjectId.whereKey("type", equalTo: "followingAction")


    findUserObjectId.findObjectsInBackgroundWithBlock { (objects:[AnyObject]!, error:NSError!) -> Void in
        if error == nil  {
            // The find succeeded.
            println("succesfully loaded the fromUser  in Activity class")
            // Do something with the found objects
            for object  in objects   {

                let user : PFUser = object["toUser"] as PFUser



                        self.followingUserList.addObject(user)
                        println("User added to following user list : \(user)")
                        println("followingUserlist = \(self.followingUserList)")

                        self.tableView.reloadData()


            } } else {
            // Log details of the failure
            println("error loadind user ")
            println(error)
        }
    }
}

如果我打印 followingUserList 这是我放置获取的用户的数组,这就是我所拥有的:

followingUserlist = (
"<PFUser: 0x7d9b93c0, objectId: DQJihBpW5E, localId: (null)> {\n}"
)

当我对常规用户表进行常规查询 (PFUser.query()) 时,我有更多详细信息:

例如:

   <PFUser: 0x7db5af80, objectId: niwftRrB5x, localId: (null)> {
backgroundImage = "<PFFile: 0x7db5f080>";
email = "kiki@kiki.com";
emailVerified = 0;
profileImage = "<PFFile: 0x7db62910>";
username = kiki;
 }

在这里我们可以看到我有完整的 PFUser,包括电子邮件、用户名等。

因为我的“Activity”表中的“fromRow”是指向我的常规 User 表的指针,为什么我的 loadUser() 方法中获取的结果不完整?

最佳答案

好吧,我终于找到了解决方案,我只是像这样使用 getObjectInBackgroundWithId 方法:

   func loadUser () {

    followingUserList.removeAllObjects()

    let findUserObjectId =       PFQuery(className: "Activity")
    findUserObjectId.whereKey("fromUser", equalTo: userPassed)
    findUserObjectId.whereKey("type", equalTo: "followingAction")


    findUserObjectId.findObjectsInBackgroundWithBlock { (objects:[AnyObject]!, error:NSError!) -> Void in
        if error == nil  {
            // The find succeeded.
            println("succesfully loaded the fromUser  in Activity class")
            // Do something with the found objects
            for object  in objects   {

                let user : PFUser = object["toUser"] as PFUser

                let queryUsers = PFUser.query()
                queryUsers.getObjectInBackgroundWithId(user.objectId, block: { (userGet :PFObject!,error : NSError!) -> Void in
                    self.followingUserList.addObject(userGet)
                    self.tableView.reloadData()

                })


            } } else {
            // Log details of the failure
            println("error loadind user ")
            println(error)
        }
    }
}

关于ios - swift + Parse.com : how to fetch user relation using a join table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26753701/

相关文章:

ios - 扩展 UITableviewController 的 xcode 键盘滚动问题

ios - 如何加速或优化 iOS 的 SQLite 查询?

ios - swift 中调用初始化程序错误消息时没有完全匹配

swift - 类型开关模式

ios - 如何反复检查互联网连接?

ios - 如何清除 swift4 中的导航栏颜色?

iphone - 如何对 iOS 应用程序使用的服务器进行负载测试?

ios - NSFetchedResultsControllerDelegate 是如何工作的?

android - Linux Ubuntu中Gradle解析Android Studio中的问题

elasticsearch - 使用 ElasticSearch 托管 : Parse. com