2016-11-29 70 views
0

我得到了Instagram的應用程序。 我火力點作爲後端其中有:Firebase複雜查詢 - iOS Swift

後節點 - 包含所有帖子來自不同用戶 [POST_ID post_user POST_NAME POST_CONTENT]

問題: 我只想從得到的帖子我跟隨的用戶。

我的步驟:

  1. 我在陣列下面users_id列表

    followersIds 
    
  2. 我想知道如果我可以讓查詢像

    getFollowersList { (followersIds) in 
    
        print(followersIds) 
    
        self.postRef.queryOrdered(byChild: "user").queryEqual(toValue: followersIds).queryLimited(toLast: limit).observeSingleEvent(of: .value, with: { (snapshot) in 
         if snapshot.value is NSNull { completion(nil); return} 
         else { 
          let dict = snapshot.value as! [String:Any] 
          self.mapDictionaryToArrayOfPost(dict: dict, completion: { (posts) in 
           completion(posts) 
          }) 
         } 
        }) { (error) in 
         debugPrint(error.localizedDescription) 
        } 
    
    } 
    

問題: 火力地堡不能接受queryEqual數組作爲參數()

我可以在應用程序過濾做這個過濾器,但我想查詢火力並得到結果。 因爲在應用程序中過濾不是一件好事。

任何建議。

回答

2

無法在Firebase中一次執行此查詢。在大多數NoSQL解決方案中很常見,您應該以允許應用程序需要的用例的方式對數據進行建模。

這意味着:如果您想要一次性檢索您關注的人的所有帖子,則應該列出您在數據庫中關注的人的所有帖子。既然你似乎是建立就像一個社交網絡,這意味着你基本上建立每個用戶的牆:

following 
    uid1 
    uid2: true // so uid1 follows uid2 and uid3 
    uid3: true 
    uid3 
    uid2: true // and uid3 follows uid2 
posts 
    post1 
     author: uid2 // uid2 has made a post 
     title: "...." 
    post2 
     author: uid3 // uid3 has made a post 
     title: "...." 
wall 
    uid1 
    post1: true 
    post2: true 
    uid3 
    post1: true 

因此,在這個模型中,我們保持一個用戶的每一個職位,你跟着/wall/myuid下的關鍵。現在,您可以輕鬆獲取帖子列表以顯示特定用戶的牆。從中你可以遍歷按鍵並加載它們中的每一個。後者對Firebase來說不是一個緩慢的操作,因爲它處理了請求。見Speed up fetching posts for my social network app by using query instead of observing a single event repeatedly

這種類型的數據複製是NoSQL數據庫,如火力地堡,這就是爲什麼我們在documentation on structuring data覆蓋它,在我們的firefeed demo app和我們的新的視頻系列Firebase for SQL developers非常普遍。