2016-05-17 104 views
1

我創建了一個Firebase Swift應用,用戶可以在其中查看由其他用戶在提要中發佈的消息。有些消息與某些用戶有關。消息可以評論和upvoted/downvoted。Firebase可擴展性:整個數據集對多個觀察者的觀察

這是一個更可擴展的設計:

1)一個觀察者,檢查所有消息。價值和重新填充料。

2)每個用戶擁有的消息的一個觀察者,並刪除並添加觀察者作爲饋送更新。

1)聽起來不太可擴展,但我擔心2)有許多觀察者和添加和刪除觀察者的延遲。

哪一個更好的設計?

回答

4

我會建議一個超級簡單的方法。將觀察者添加到包含相關用戶的消息中

因此,例如,這裏是存儲帖子的帖子(消息)節點。

{ 
    "posts" : { 
    "post_0" : { 
     "msg" : "some message", 
     "user" : "uid_0" 
    }, 
    "post_1" : { 
     "msg" : "another message", 
     "user" : "uid_1" 
    }, 
    "post_2" : { 
     "msg" : "yippee message", 
     "user" : "uid_2" 
    } 
    } 
} 

而這裏的觀察,其中包括一個參考任何職位代碼一旦這個代碼運行到uid_2

let postsRef = myRootRef.childByAppendingPath("posts") 
     postsRef.queryOrderedByChild("user").queryEqualToValue("uid_2") 
      .observeEventType(.ChildAdded, withBlock: { snapshot in 

     print(snapshot) 

    }) 

,被張貼在有用戶的帖子節點的任何職位= uid_2會通知該用戶。

您可以通過更改爲多個用戶

"post_0" : { 
     "msg" : "some message", 
     "users_watching_this_post" 
     "uid_0": true 
     "uid_2": true 
    }, 

的post_x節點和深查詢這個擴大

postsRef.queryOrderedByChild("users_watching_this_post/uid_2").queryEqualToValue(true) 
     .observeEventType(.ChildAdded, withBlock: { snapshot in 
    print(snapshot) 
})