swift - 关系 - Parse 中的一对多

标签 swift parse-platform relational-database

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

对象 - 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/

相关文章:

swift - 如何在更改 SpriteKit 中的变量之前创建延迟

swift - 如何找出某个 OSX 版本可用的 API 版本?

ios - 如何访问保存在 for 循环中的数组? ( objective-c )

database - 规范化和主键

mysql - 使用 SELECT 有条件地填充列

SQL 智能连接

ios - 去除导航栏下的细线

json - 具有不一致 API 的 Swift Decodable

mysql - 解析文件路径 - 一列多行

ios - 如何从 Parse 中删除对象?