解析服务器中的数据结构:
对象 - objectId:String,userPointer:指针,标题:字符串,图像:文件,描述:字符串,已验证:Bool,注释:关系(当我点击评论类时的关系)它加载相关的评论)
评论 - objectId:String,userPointer:Pointer,imagePointer:Pointer,comment:String
我正在尝试将评论保存到评论类。 同时我需要将Relation添加到Objects类 ............目的是当您查询关系时,您将获得与之关联的所有注释。
到目前为止,我已尝试过:
// Create the comment
let myComment = PFObject(className:"Comments")
myComment["comment"] = "kgvhgv"
// Add a relation between the Post and Comment
myComment["userPointer"] = PFObject(withoutDataWithClassName: "_User", objectId: "nnnnnnnnn")
myComment["imagePointer"] = PFObject(withoutDataWithClassName: "Objects", objectId: "nnnnnnnnn")
let query = PFQuery(className:"Objects")
query.whereKey("objectId", equalTo: "tJP0kskNxQ")
query.findObjectsInBackground { (objects, error) -> Void in
if (error == nil) {
for object in objects! {
print(object)
var relation = object.relation(forKey: "comments")
relation.add(myComment)
relation.saveInBackground(block: { (success:Bool, error:Error?) -> Void in
if (success) {
print("Saved relation")
// The post has been added to the user's likes relation.
} else {
print(error)
// There was a problem, check error.description
}
})
}
}
}
// This will save both myPost and myComment
myComment.saveInBackground()
}
和
let object = PFObject(withoutDataWithClassName: "Objects", objectId: "nnnnnnnnn")
var relation = object.relationForKey("comment")
relation.addObject(myComment)
m.saveInBackgroundWithBlock {
(success: Bool, error: NSError?) -> Void in
if (success) {
// The post has been added to the user's likes relation.
} else {
// There was a problem, check error.description
}
}
注释正确地保存了指针而不是关系。
我能找到的所有教程都与保存与用户的关系有关,但这不是我想要的。
非常感谢。
答案 0 :(得分:0)
.....结果解决方法是放
var relation = object.relationForKey("comment")
relation.addObject(myComment)
在saveInBackGround
区块内!