iosswiftfirebase-realtime-databasedata-objects

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. Since FirebaseDatabase uses JSON/NoSQL that's not possible. 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?

UserEntity Object:

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:

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

Solution

  • It would be better if you could structure your data in Firebase. Here is the links that provide nice intuition about structuring data in Firebase:

    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{
        "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.

    You have to update the posts attribute of user table as p5 :true, when a new post p5 is added by user u1.