【问题标题】:How to associate to different data objects in Firebase 3 with each other using Swift?如何使用 Swift 将 Firebase 3 中的不同数据对象相互关联?
【发布时间】:2016-11-19 16:01:29
【问题描述】:

我知道在 SQL 中将 2 个不同的对象相互关联,其中一个将使用 1 个表中的主键和另一个表中的外键。由于 FirebaseDatabase 使用 JSON/NoSQL 这是不可能的。如果我有 2 个对象是 UserPostEntity 和 PostEntity,一旦用户发表了帖子/评论,我将如何将 UserPostEntity 与 PostEntity 关联,以及如何使用用户发表的帖子/评论自动更新 PostEntity?

用户实体对象:

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

【问题讨论】:

  • 你的 UserEntityPostEntity 有什么区别?它们具有相同的属性!

标签: ios swift firebase-realtime-database data-objects


【解决方案1】:

如果您可以在 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
    }
}

此外,您还想按照post 中的回答构建您的UserEntityPostEntity 实体。

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

【讨论】:

  • 您好,感谢您的帮助。快速提问,参考的目的是什么:FIRDatabaseReference?它们都设置为零,那么目的是什么?
  • ref 用于跟踪对数据库中数据所在路径的引用。例如UserEntity 的情况是user 表的路径,PostEntity 的情况是post 表的路径。
  • ref 将在您从 firebase 检索数据时提供帮助。对于每个实体,您不必查找参考。它将作为其属性呈现。
猜你喜欢
  • 2020-11-27
  • 2011-08-22
  • 1970-01-01
  • 1970-01-01
  • 2017-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多