QueryEndingAtValue函数在Swift firebase中无法正常工作

时间:2018-05-19 11:20:29

标签: ios swift firebase firebase-realtime-database

我一直在实现代码,以便通过firebase数据库中的一定数量的数据启用分页滚动来获取数据。

首先,然后错误说

  

由于未捕获的异常'InvalidQueryParameter'而终止应用,   原因:'不能使用queryEndingAtValue:与字符串中的其他类型   与queryOrderedByKey'

组合

以下是产生上述错误的实际代码

static func fetchPostsWith(last_key: String?, completion: @escaping (([PostModel]?) -> Void)) {

    var posts = [PostModel]()
    let count = 2

    let ref = Database.database().reference().child(PATH.all_posts)
    let this_key = UInt(count + 1)
    let that_key = UInt(count)

    let this = ref.queryOrderedByKey().queryEnding(atValue: last_key).queryLimited(toLast: this_key)
    let that = ref.queryOrderedByKey().queryLimited(toLast: that_key)

    let query = (last_key != nil) ? this : that

    query.observeSingleEvent(of: .value) { (snapshot) in
        if snapshot.exists() {
            for snap in snapshot.children {
                if let d = snap as? DataSnapshot {
                    let post = PostModel(snapshot: d)
                    print(post.key ?? "")
                    posts.append(post)
                }
            }
            posts.sort { $0.date! > $1.date! }
            posts = Array(posts.dropFirst())
            completion(posts)
        } else {
            completion(nil)
        }
    }
}

它尝试做的是获取自动ID存储所有帖子的路径。但错误不断出现所以我不知道出了什么问题。你有什么想法吗?

 func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    // index is the last and last fetched then
    print(self.feeds.count - 1 == indexPath.row, "index ", self.hasFetchedLastPage, "has fetched last")
    if self.feeds.count - 1 == indexPath.row {
        let lastKey = self.feeds[indexPath.row].key
        if lastKey != self.previousKey {
            self.getFeeds(lastKey: lastKey)
        } else {
            self.previousKey = lastKey ?? ""
        }
    }
    print("Cell Display Number", indexPath.row)
}

func getFeeds(lastKey: String?) {
    print(self.isFetching, "is fetching")
    guard !self.isFetching else {
        self.previousKey = ""
        return
    }
    self.isFetching = true
    print(self.isFetching, "is fetching")
    FirebaseModel.fetchPostsWith(last_key: lastKey) { ( d ) in
        self.isFetching = false
        if let data = d {
            if self.feeds.isEmpty { //It'd be, when it's the first time.
                self.feeds.append(contentsOf: data)
                self.tableView.reloadData()
                print("Initial Load", lastKey ?? "no key")
            } else {
                self.hasFetchedLastPage = self.feeds.count < 2
                self.feeds.append(contentsOf: data)
                self.tableView.reloadData()
                print("Reloaded", lastKey ?? "no key")
            }
        }
    }
}

我想实现一个支持分页的tableView。如果您可以帮助此代码正常工作,我们非常感谢。

1 个答案:

答案 0 :(得分:2)

您将last_key转换为数字,而键始终是字符串。该错误消息告诉您这两种类型不兼容。这意味着在将代码传递给查询之前,必须将代码转换回代码中的字符串:

let this = ref.queryOrderedByKey().queryEnding(atValue: last_key).queryLimited(toLast: String(this_key))
let that = ref.queryOrderedByKey().queryLimited(toLast: String(that_key))