无论如何使用解析将对象保存到另一个用户?

时间:2015-08-06 16:48:05

标签: swift parse-platform pfuser

我正在尝试创建一个可以关注用户的应用,我可以将我想要关注的用户的userId保存到用户类中的“跟随”键。但是,我无法将当前userId保存为被关注用户的“关注者”键。我收到错误“用户无法保存,除非他们已通过logIn或signUp进行身份验证”

func followUser() {
    //User to be followed's username
    var userName = self.title!
    let myUserId = PFUser.currentUser()?.objectId




    var query = PFUser.query()
    query?.whereKey("objectId", equalTo: myUserId!)
    query?.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
        if let users = objects {
            for user in users {
                //thisUserId = objectId of user being followed
                user.addUniqueObject(self.thisUserId, forKey: "following")
                user.saveInBackgroundWithBlock({ (success, error) -> Void in
                    if error != nil {
                        println(error)
                    } else {
                            println("You favorited \(self.title)")
                            var otherQuery = PFUser.query()
                            otherQuery?.whereKey("objectId", equalTo: self.thisUserId)
                            otherQuery?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
                                if let users = objects {
                                    for object in users {
                                        if let user = object as? PFUser {
                                            user.addUniqueObject(myUserId!, forKey: "followers")
                                            user.saveInBackgroundWithBlock({ (success, error) -> Void in
                                                if error == nil {
                                                    println("You are following \(self.title)")

                                                } else {
                                                    println(error)
                                                }
                                            })
                                        }
                                    }
                                }
                            })

                    }
                })
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

解析网站似乎有更好的处理能力。您的代码没有任何问题(虽然它是嵌套的,可以使用一些功能抽象)

https://parse.com/questions/user-cannot-be-saved-unless-they-have-been-authenticated-via-login-or-signup