簡體   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