gpt4 book ai didi

ios - FirebaseDatabase - 使用 UISearchController 时如何分页

转载 作者:行者123 更新时间:2023-11-29 05:30:53 25 4
gpt4 key购买 nike

当用户在 searchBar 中键入文本时,UISearchController 有一个委托(delegate)方法来更新搜索结果:

func updateSearchResults(for searchController: UISearchController) {

guard let searchText = searchController.searchBar.text?.lowercased() else { return }

Database...usersRef
.queryOrdered(byChild: "username")
.queryStarting(atValue: searchText)
.queryEnding(atValue: searchText+"\u{f8ff}")
.observe( .childAdded, with: { [weak self](snapshot) in

let key = snapshot.key

guard let dict = snapshot.value as? [String: Any] else { return }

let user = User(userId: key, dict: dict)
self?.datasource.append(user)
})
}

效果很好。

当我通常分页时,我使用这个程序:

var startKey: String?

func handlePaginationForPosts() {

if startKey == nil {

Database...PostsRef
.queryOrderedByKey()
.queryLimited(toLast: 10)
.observeSingleEvent(of: .value, with: { [weak self] (snapshot) in

guard let children = snapshot.children.allObjects.first as? DataSnapshot else { return }

if snapshot.childrenCount > 0 {
for child in snapshot.children.allObjects as! [DataSnapshot] {

let postId = child.key

if child.key != self?.startKey {

guard let dict = child.value as? [String:Any] else { return }

let post = Post(postId: postId, dict: dict)

self?.datasource.insert(post, at: 0)
}
}
self?.startKey = children.key
}
})

} else {

let lastIndex = datasource.count

Database...PostsRef
.queryOrderedByKey()
.queryEnding(atValue: startKey!)
.queryLimited(toLast: 11)
.observeSingleEvent(of: .value, with: { [weak self] (snapshot) in

guard let children = snapshot.children.allObjects.first as? DataSnapshot else { return }

if snapshot.childrenCount > 0 {
for child in snapshot.children.allObjects as! [DataSnapshot] {

let postId = child.key

if child.key != self?.startKey {

guard let dict = child.value as? [String:Any] else { return }

let post = Post(postId: postId, dict: dict)
// I run a check to make sure the datasource doesn't contain the post before adding it
self?.datasource.insert(post, at: lastIndex)
}
}
self?.startKey = children.key
}
})
}
}

这里的问题是运行我使用的搜索时:

.queryStarting(atValue: searchText)
.queryEnding(atValue: searchText+"\u{f8ff}")

但是在对帖子进行分页时我使用:

.queryOrderedByKey()
.queryEnding(atValue: startKey!) ...

self?.datasource.insert(post, at: lastIndex)

startKeysnapshot.children.allObjects.first 中的第一个键,lastIndexdatasource.count .

考虑到搜索查询是基于搜索文本而不是 key,当我已经使用 .queryEnding(atValue: searchText+"\u{f8ff} 时如何进行分页") 而不是 .queryEnding(atValue: startKey!)

我需要跟踪从数据库中提取的 key ,以便在分页时可以从该特定 key 运行下一组结果。

最佳答案

Firebase 数据库查询只能对单个属性进行排序/过滤。

因此,您可以做的是过滤搜索条件,然后限制为前 N 个结果。

不能做的是过滤搜索条件、跳过前 N 个结果并获取下一页。

您能得到的最接近的结果,以及针对此类情况定期执行的操作,是在需要显示第 2 页时检索前 2*N 个结果。但这会浪费一些带宽,因此您必须对此进行权衡对比分页的有用性。

关于ios - FirebaseDatabase - 使用 UISearchController 时如何分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57592977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com