关系 - Parse 中的一对多
Relations - One to many in Parse
解析服务器中的数据结构:
对象 - objectId:String, userPointer:Pointer, title:String, image:File, description:String, verified:Bool、comments:Relations(关系,这样当我点击评论时 class 它会加载关联的评论)
评论 - objectId:String, userPointer:Pointer, imagePointer:Pointer, comment:String
我正在尝试将评论保存到评论 class。
同时我需要将关系添加到对象 Class
...................目的是当您查询关系时,您会得到与之关联的所有评论。
到目前为止我已经尝试过:
// 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
}
}
注释正确地保存了指针,但是关系没有。
我能找到的所有教程都与保存与用户的关系有关,但这不是我想要的。
非常感谢帮助。
.....原来解决方法是把
var relation = object.relationForKey("comment")
relation.addObject(myComment)
在 saveInBackGround
块内!
解析服务器中的数据结构:
对象 - objectId:String, userPointer:Pointer, title:String, image:File, description:String, verified:Bool、comments:Relations(关系,这样当我点击评论时 class 它会加载关联的评论)
评论 - objectId:String, userPointer:Pointer, imagePointer:Pointer, comment:String
我正在尝试将评论保存到评论 class。 同时我需要将关系添加到对象 Class ...................目的是当您查询关系时,您会得到与之关联的所有评论。
到目前为止我已经尝试过:
// 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
}
}
注释正确地保存了指针,但是关系没有。
我能找到的所有教程都与保存与用户的关系有关,但这不是我想要的。
非常感谢帮助。
.....原来解决方法是把
var relation = object.relationForKey("comment")
relation.addObject(myComment)
在 saveInBackGround
块内!