Firebase提取按值排序只有20个第一个条目

时间:2017-03-20 17:25:16

标签: ios swift firebase firebase-realtime-database

我正在创建一个论坛,该论坛将拥有由用户创建的无限数量的主题。所以对于性能我一次只能获取20个左右的主题。当用户访问表格视图的底部时,我们应该再获取20个,依此类推。

问题是我正在对主题进行排序后,我从Firebase获取所有这些主题,并且我很难直接在Firebase上正确地订购它们,然后我才能获取它们,这是我能看到这种工作的唯一方法。 / p>

主题按“最后发布”排序,这是Firebase上的一个字符串,我将其转换为日期并在从Firebase中提取后重新排序。

   func fetchTopicsFromFirebase(){

    guard let cat = self.currentCategory else {return}

    DataService.ds.Topics_Base.child(cat.key).observe(.value, with: { (snapshot) in
        if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {//snapshots = all topics
            self.allTopics = []

            for top in snapshots{

                if let dict = top.value as? Dictionary<String,AnyObject>{

                    guard let title = dict["title"] as? String else {continue}
                    guard let text = dict["text"] as? String else {continue}
                    guard let time = dict["time"] as? String else {continue}
                    guard let username = dict["username"] as? String else {continue}

                    let newTopic = Topic(subject: title, text: text, time: time, topicKey: top.key, username: username)

                    if let lastPost = dict["last-post"] as? String{
                    newTopic.lastPostTime = HelperMethods.convertStringToDate(myString: lastPost)
                    }
                    if let email = dict["email"] as? String{
                        let validEmail = HelperMethods.removeSpecialCharactersFromEmail(email: email)
                        DataService.ds.Users_Base.child(validEmail).observeSingleEvent(of: .value, with: {(snapshot) in
                            if let userDict = snapshot.value as? Dictionary<String,AnyObject>{
                                if let imgUrl = userDict["profile_image_url"] as? String{
                                    newTopic.opImageUrl = imgUrl
                                 }
                                self.allTopics.append(newTopic)
                                self.allTopics.sort { $0.lastPostTime! > $1.lastPostTime!}//lastPostTime is a Date object

                                if (self.allTopics.count == snapshots.count){
                                self.tableView.reloadData()
                                }
                            }
                        })
                    }

                }
            }
        }

               }
    )
}

提前感谢您的帮助。

0 个答案:

没有答案