繁体   English   中英

如何使用Swift将Firebase 3中的不同数据对象彼此关联?

[英]How to associate to different data objects in Firebase 3 with each other using Swift?

我知道在SQL中将2个不同的对象彼此关联会在一个表中使用主键,在另一个表中使用外键。 由于FirebaseDatabase使用JSON / NoSQL,因此不可能。 如果我有2个对象,分别为UserPostEntity和PostEntity,那么当用户发表帖子/评论时,我如何将UserPostEntity与PostEntity关联起来,以及如何使用该用户发表的帖子/评论来自动更新PostEntity?

UserEntity对象:

import Foundation

    class UserEntity{

        var userID: String
        var postedComment: String
        var postDate: String
        var postID: PostEntity

        init(userID: String, postedComment: String, postDate: String, postID: PostEntity){
            self.userID = userID
            self.postedComment = postedComment
            self.postDate = postDate
            self.postID = postID
        }
    }

PostEntity对象:

import Foundation

class PostEntity{

    var userID: String
    var postID: String
    var postedComment: String
    var postDate: String

    init(userID: String, postID: String, postedComment: String, postDate: String){
        self.userID = userID
        self.postID = postID
        self.postedComment = postedComment
        self.postDate = postDate
    }
}

如果可以在Firebase中构建数据,那会更好。 以下是提供有关Firebase中数据结构的直观知识的链接:

https://firebase.googleblog.com/2013/04/denormalizing-your-data-is-normal.html https://www.firebase.com/docs/web/guide/structuring-data.html

您可以如下定义user表和posts

user{
    "u1" {

         userName : "abc",
         posts {
              p1 : true,
              p2 : true
          }
        },

        "u2" {

         userName : "def",
         posts {
              p3 : true,
              p4 : true
          }
        }

  }

  post{
        "p1"{
            userId : "u1",
            postComment : "hello ios",
            postDate : "1467570919"
            },
        "p2"{
            userId : "u1",
            postComment : "ios",
            postDate : "1467570920"
            },
        "p3"{
            userId : "u2",
            postComment : "hello ios",
            postDate : "1467570921"
            },
        "p4"{
            userId : "u2",
            postComment : "hello ios",
            postDate : "1467570922"
            }
    }

您还可以按照以下步骤创建实体:

class UserEntity{

        var userID: String
        var userName : String
        var posts: Dictionary<String, Bool>?
        var ref : FIRDatabaseReference?

        init(userID: String, userName: String, posts: Dictionary<String, Bool>?){
            self.userID = userID
            self.userName = userName
            self.posts = posts
            self.ref = nil
        }
    }


    class PostEntity{

    var pId: String
    var uId: String
    var postedComment: String
    var postDate: NSTimeInterval
    var ref : FIRDatabaseReference?

    init(pId: String, uId: String, postedComment: String, postDate: NSTimeInterval){
        self.pId= pId
        self.uId = uId
        self.postedComment = postedComment
        self.postDate = postDate
        self.ref = nil
    }
}

你也想构建你的UserEntityPostEntity在此回答实体职务

当用户u1添加新帖子p5时,必须将user表的posts属性更新为p5 :true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM