gpt4 book ai didi

swift - 关系 - Parse 中的一对多

转载 作者:行者123 更新时间:2023-11-30 12:25:01 24 4
gpt4 key购买 nike

解析服务器中的数据结构:

对象 - objectId:String,userPointer:Pointer,title:String,image:File,description:String,verified:Bool,comments:Relations(关系,以便当我单击注释类时它加载相关评论)

评论 - objectId:String, userPointer:Pointer, imagePointer:Pointer, comment:String

我正在尝试将评论保存到评论类中。同时我需要将关系添加到对象类 ............目的是当您查询关系时,您会获得与其关联的所有评论。

到目前为止我已经尝试过:

// 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 block 内!

关于swift - 关系 - Parse 中的一对多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44320500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com