如何使用Swift将Firebase 3中的不同数据对象相互关联



我知道在SQL中,要将两个不同的对象相互关联,其中一个将在一个表中使用主键,另一个表中将使用外键。由于FirebaseDatabase使用JSON/NoSQL,这是不可能的。如果我有两个对象,分别是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.htmlhttps://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

相关内容

  • 没有找到相关文章

最新更新