gpt4 book ai didi

ios - 处理复杂的 Firebase 数据库查询的 null 返回

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

我的应用内置了一个搜索功能,可以从我的 Firebase 数据库中过滤用户。

我能够在一定程度上正确地做到这一点。问题是每当我搜索不存在的用户时,应用程序就会崩溃并出现错误:索引超出范围

这些是 UICollectionViewCells,所以我处理 numberOfItemsInSection 返回的单元格,我以为我已经为条件 nil 或零进行了编码,但显然不是。

这里是执行搜索的地方

func searchUsers(searchText: String) {

let ref = Database.database().reference()
ref.child("users").queryOrdered(byChild: "name").queryStarting(atValue: searchText).queryEnding(atValue: "\(searchText)\u{f8ff}")
.observe(.childAdded, with: {(snapshot) in

if let dictionary = snapshot.value as? [String: AnyObject] {
let user = User()
user.setValuesForKeys(dictionary)

self.users.append(user)
print(snapshot)

DispatchQueue.main.async(execute: {
self.collectionView?.reloadData()
})
}

print(self.users.count)
}, withCancel: nil)

}

有什么建议吗?

最佳答案

您不应从 numberOfItemsInSection 返回 Int(),而是返回 0

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
switch section {
case 0:
return users.count
case 1:
// handle based on section 1 datasource count
}

关于ios - 处理复杂的 Firebase 数据库查询的 null 返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45261111/

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