gpt4 book ai didi

iOS Firebase - 向下查询多个级别的最佳方式是什么? 'InvalidQueryParameter' , 原因 : 'Cannot use multiple queryOrderedBy calls!'

转载 作者:行者123 更新时间:2023-11-28 07:43:16 25 4
gpt4 key购买 nike

我正在使用 UISearchController 并且我想在注释键上查询从根向下的 2 个节点。它将是 root > posts > uid > postId > comment

root
|
@--posts
|
@--uid123
|
@--postId987
|
|-comment: "I like pizza"

我尝试链接多个查询 let commentRef = postsRef.queryOrderedByKey().queryOrdered(byChild: "comment").queryStarting(atValue: searchText).queryEnding(atValue: searchText+"\u{f8ff}" 但我遇到了崩溃:

*** Terminating app due to uncaught exception 'InvalidQueryParameter', reason: 'Cannot use multiple queryOrderedBy calls!'

向下查询多个级别的最佳方式是什么?

导致崩溃的代码:

func updateSearchResults(for searchController: UISearchController) {

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

let postsRef = Database.database().reference().child("posts")

let commentRef = postsRef.queryOrderedByKey().queryOrdered(byChild: "comment").queryStarting(atValue: searchText).queryEnding(atValue: searchText+"\u{f8ff}")

commentRef.observeSingleEvent(of: .value, with: { (snapshot) in

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

dictionaries.forEach({ (key, value) in

guard let dict = value as? [String: Any] else { return }
let post = Post(dict: dict)

let isContained = self.filteredComments.contains(where: { (containedPost) -> Bool in
return post.comment == containedPost.comment
})
if !isContained {
self.filteredComments.append(post)
self.collectionView?.reloadData()
}
})
})
}

我在下面找到了另一种方法。我首先在 posts ref 上获取 .value,遍历所有子项,然后将每个 snapshot.key 作为子项附加到 postsRef 并从那里获取评论.它确实有效。问题是它似乎效率不高,尤其是当有数百万的帖子和/或用户需要整理时。

let postsRef = Database.database().reference().child("posts")

postsRef.observeSingleEvent(of: .value) { (snapshot) in

for child in snapshot.children {

let uid = child as! DataSnapshot

let commentRef = postsRef.child(uid.key).queryOrdered(byChild: "comment").queryStarting(atValue: searchText).queryEnding(atValue: searchText+"\u{f8ff}")

commentRef.observeSingleEvent(of: .value, with: { (snapshot) in
// the rest of the code
}

最佳答案

我将把它作为一个可能的解决方案抛出。

目前的结构为何如此还不是很清楚,但我建议将其更改为

post_comments
post_id_x
posted_by: "uid1234"
comment: "I like pizza"

如果结构需要保持原样,那么只需将其添加为另一个节点,这将使您的评论查询更加简单。

关于iOS Firebase - 向下查询多个级别的最佳方式是什么? 'InvalidQueryParameter' , 原因 : 'Cannot use multiple queryOrderedBy calls!' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51642330/

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