我正在尝试创建一个可以关注用户的应用,我可以将我想要关注的用户的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)
}
})
}
}
}
})
}
})
}
}
}
}
答案 0 :(得分:0)
解析网站似乎有更好的处理能力。您的代码没有任何问题(虽然它是嵌套的,可以使用一些功能抽象)