简体   繁体   English

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

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

I know in SQL to associate 2 different objects with each other one would use a primary key in 1 table and a foreign key in another table. 我知道在SQL中将2个不同的对象彼此关联会在一个表中使用主键,在另一个表中使用外键。 Since FirebaseDatabase uses JSON/NoSQL that's not possible. 由于FirebaseDatabase使用JSON / NoSQL,因此不可能。 If I had 2 objects being UserPostEntity and PostEntity, once a user made a post/comment how would I associate a UserPostEntity with a PostEntity and how would the PostEntity be automatically updated with that post/comment the user made? 如果我有2个对象,分别为UserPostEntity和PostEntity,那么当用户发表帖子/评论时,我如何将UserPostEntity与PostEntity关联起来,以及如何使用该用户发表的帖子/评论来自动更新PostEntity?

UserEntity Object: 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 Object: 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
    }
}

It would be better if you could structure your data in Firebase. 如果可以在Firebase中构建数据,那会更好。 Here is the links that provide nice intuition about structuring data in Firebase: 以下是提供有关Firebase中数据结构的直观知识的链接:

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

You can define your user table and posts as follow: 您可以如下定义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"
            }
    }

Also you can creates your entities as follow: 您还可以按照以下步骤创建实体:

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
    }
}

Also you would want to structure your UserEntity and PostEntity entity as answered in this post . 你也想构建你的UserEntityPostEntity在此回答实体职务

You have to update the posts attribute of user table as p5 :true , when a new post p5 is added by user u1 . 当用户u1添加新帖子p5时,必须将user表的posts属性更新为p5 :true

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

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