2017-04-22 63 views
3

我使用firebase編碼購物網站。 我有一些類別和子類別在側邊欄,onClick他們,帖子提要應相應更新,按時間排序,只顯示選定類別的帖子。在火力數據庫如何分頁包含equalTo()api的firebase查詢

後對象:

-KiKGQUZ4XaZPHxvOZD-addclose { 
     categorie:0 
     subCat: 2 
     .... 
    } 

有很多帖子,我需要分頁他們

要做到這一點,我應該這樣的查詢:

dbRef 
    .orderByChild('categories') 
    .equalTo(category) 
    .limitToLast(QUERY.PAGINATION_COUNT) 
    .on('value', (snapshot) => { 
     cb(_toArray(snapshot).reverse()); 
    }); 

但我無法與equalTo()分頁。

我認爲解決的辦法應該是:

dbRef 
    .orderByChild('categories') 
    .equalTo(category) 
    .startAt(lastFetchedItem.fbKey) 
    .limitToLast(QUERY.PAGINATION_COUNT) 
    .on('value', (snapshot) => { 
     cb(_toArray(snapshot).reverse()); 
    }); 

但它不是用火力可以做到equalTo查詢和startAt的同時

如何,我可以解決這個問題?

回答

0

我最終更改了firebase實時數據庫模式。

我不確定,但我認爲firebase有意不釋放更多apis。它迫使你仔細檢查你的模式並改進它。

我已經創建了專門的類別集合和類別集合,爲每個類別創建了單獨的節點。 它用什麼firebase recommend來構建你的數據庫。

然後將每個新帖子放入相應的類別。

所以我可以輕鬆地使用在每個類別和子類別​​和使用startAtendAt的API

的另一個解決方案是使用火力的功能和彈性的搜索分頁他們。已爲此創建了 FlashLight package

0

基於this source它看起來像這個功能是不可能的,不會被添加。您可以使用equalTo

  • 你的最後一個可見密鑰查詢的關鍵結局做這樣的事情,雖然

    1. 查詢您最初的尋呼量,如果該類別相匹配你想要然後將其添加了一個過濾結果,否則忽略它。

    我在Swift3中編寫了我的代碼,但也有同樣的問題,也許我的代碼會對其他人有所幫助。

    static func filterTableByCategory(tableNumber: String, category: String) { 
         filteringRef.child(tableNumber).queryOrdered(byChild: "category").queryEqual(toValue: category).queryLimited(toLast: 9).observeSingleEvent(of: .value, with: { snap in 
    
          if snap.exists() { 
           currentTablePosts.removeAll() 
           for child in snap.children { 
            let child = child as? DataSnapshot 
            if let post = child?.value as? [String: AnyObject] { 
             let posst = Post() 
             if let author = post["author"] as? String, let likes = post["likes"] as? Int, let pathToImage = post["pathToImage"] as? String, let postID = post["postID"] as? String, let postDescription = post["postDescription"] as? String, let timestamp = post["timestamp"] as? Double, let category = post["category"] as? String, let table = post["group"] as? String, let userID = post["userID"] as? String, let numberOfComments = post["numberOfComments"] as? Int, let region = post["region"] as? String { 
    
              posst.author = author 
              posst.likes = likes 
              posst.pathToImage = pathToImage 
              posst.postID = postID 
              posst.userID = userID 
              posst.fancyPostDescription = Helper.createAttributedString(author: author, postText: postDescription) 
              posst.postDescription = author + ": " + postDescription 
              posst.timestamp = timestamp 
              posst.table = table 
              posst.region = region 
              posst.category = category 
              posst.numberOfComments = numberOfComments 
              posst.userWhoPostedLabel = Helper.createAttributedPostLabel(username: author, table: table, region: region, category: category) 
    
              if let people = post["peopleWhoLike"] as? [String: AnyObject] { 
               for(_, person) in people { 
                posst.peopleWhoLike.append(person as! String) 
               } 
              } 
              currentTablePosts.insert(posst, at: 0) 
              NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadTableCollectionView"), object: nil) 
             } // end of if let 
            } 
           } 
          } 
          else { 
           currentTablePosts.removeAll() 
           NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadTableCollectionView"), object: nil) 
          } 
         }) 
         filteringRef.removeAllObservers() 
        } 
    
        static func getMoreFilterByCategoryPosts(tableNumber: String, category: String, lastVisibleKey: String) { 
    
         pagingReference.child(tableNumber).queryOrderedByKey().queryEnding(atValue: lastVisibleKey).queryLimited(toLast: 12).observeSingleEvent(of: .value, with: { snap in 
    
          if snap.exists() { 
           let currentNumberOfPosts = currentTablePosts.count 
           for child in snap.children { 
            let child = child as? DataSnapshot 
            if let post = child?.value as? [String: AnyObject] { 
             if let cat = post["category"] as? String, let postID = post["postID"] as? String { 
              if cat == category && postID != lastVisibleKey { 
               let posst = Post() 
               if let author = post["author"] as? String, let likes = post["likes"] as? Int, let pathToImage = post["pathToImage"] as? String, let postDescription = post["postDescription"] as? String, let timestamp = post["timestamp"] as? Double, let table = post["group"] as? String, let userID = post["userID"] as? String, let numberOfComments = post["numberOfComments"] as? Int, let region = post["region"] as? String { 
    
                posst.author = author 
                posst.likes = likes 
                posst.pathToImage = pathToImage 
                posst.postID = postID 
                posst.userID = userID 
                posst.fancyPostDescription = Helper.createAttributedString(author: author, postText: postDescription) 
                posst.postDescription = author + ": " + postDescription 
                posst.timestamp = timestamp 
                posst.table = table 
                posst.region = region 
                posst.category = cat 
                posst.numberOfComments = numberOfComments 
                posst.userWhoPostedLabel = Helper.createAttributedPostLabel(username: author, table: table, region: region, category: category) 
    
                if let people = post["peopleWhoLike"] as? [String: AnyObject] { 
                 for(_, person) in people { 
                  posst.peopleWhoLike.append(person as! String) 
                 } 
                } 
                currentTablePosts.insert(posst, at: currentNumberOfPosts) 
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadTableCollectionView"), object: nil) 
               } // end of if let 
              } 
             } 
            } 
           } 
          } 
          else { 
           print("snap == nil") 
          } 
    
         }) 
         pagingReference.removeAllObservers() 
    
        } 
    
  • +0

    感謝您的評論..但您的解決方案不適用 –

    +0

    因爲您可能會忽略實際項目中超過100個請求的響應。 –