【问题标题】:Swift + Parse.com : how to fetch user relation using a join tableSwift + Parse.com:如何使用连接表获取用户关系
【发布时间】:2014-11-05 09:35:38
【问题描述】:

我有一个像 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 = (
"<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() 方法中获取的结果不完整?

【问题讨论】:

    标签: ios swift parse-platform pfuser


    【解决方案1】:

    好的,我终于找到了解决方案,我只是像这样使用 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)
            }
        }
    }
    

    【讨论】:

    • “followingUserList”是您在代码中的某个位置初始化的数组吗?
    猜你喜欢
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 1970-01-01
    • 2016-05-16
    • 2016-07-03
    • 2021-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多