Swift-Firebase-订单收集 [英] Swift - Firebase - order collection
问题描述
我正在尝试检索一些文档,但是我需要通过愿望清单"-collection
中的某些数据("ListIDX")对它们进行排序.
I am trying to retrieve some documents but I need them to be ordered by some data ("ListIDX") inside my "Wishlists" - collection
.
我尝试了此操作,但这是不允许的:
I tried this but that's not allowed:
db.collection("users").document(userID).collection("wishlists").order(by: "ListIDX").document(list.name).collection("wünsche").getDocuments()
这是我的function
:
func getWishes (){
let db = Firestore.firestore()
let userID = Auth.auth().currentUser!.uid
var counter = 0
for list in self.dataSourceArray {
print(list.name) // -> right order
db.collection("users").document(userID).collection("wishlists").document(list.name).collection("wünsche").getDocuments() { ( querySnapshot, error) in
print(list.name) // wrong order
if let error = error {
print(error.localizedDescription)
}else{
// DMAG - create a new Wish array
var wList: [Wish] = [Wish]()
for document in querySnapshot!.documents {
let documentData = document.data()
let wishName = documentData["name"]
wList.append(Wish(withWishName: wishName as! String, checked: false))
}
// DMAG - set the array of wishes to the userWishListData
self.dataSourceArray[counter].wishData = wList
counter += 1
}
}
}
}
这实际上是我最终想要实现的:
This is what I actually would like to achieve in the end:
self.dataSourceArray[ListIDX].wishData = wList
更新
Update
我还有一个函数,可以按正确的顺序检索我的wishlists
.也许我可以在其中添加getWishes
,以便它也以正确的顺序排列.
I also have a function that retrieves my wishlists
in the right order. Maybe I can add getWishes
in there so it is in the right order as well.
func retrieveUserDataFromDB() -> Void {
let db = Firestore.firestore()
let userID = Auth.auth().currentUser!.uid
db.collection("users").document(userID).collection("wishlists").order(by: "listIDX").getDocuments() { ( querySnapshot, error) in
if let error = error {
print(error.localizedDescription)
}else {
// get all documents from "wishlists"-collection and save attributes
for document in querySnapshot!.documents {
let documentData = document.data()
let listName = documentData["name"]
let listImageIDX = documentData["imageIDX"]
// if-case for Main Wishlist
if listImageIDX as? Int == nil {
self.dataSourceArray.append(Wishlist(name: listName as! String, image: UIImage(named: "iconRoundedImage")!, wishData: [Wish]()))
// set the drop down menu's options
self.dropDownButton.dropView.dropDownOptions.append(listName as! String)
self.dropDownButton.dropView.dropDownListImages.append(UIImage(named: "iconRoundedImage")!)
}else {
self.dataSourceArray.append(Wishlist(name: listName as! String, image: self.images[listImageIDX as! Int], wishData: [Wish]()))
self.dropDownButton.dropView.dropDownOptions.append(listName as! String)
self.dropDownButton.dropView.dropDownListImages.append(self.images[listImageIDX as! Int])
}
// // create an empty wishlist
// wList = [Wish]()
// self.userWishListData.append(wList)
// reload collectionView and tableView
self.theCollectionView.reloadData()
self.dropDownButton.dropView.tableView.reloadData()
}
}
self.getWishes()
}
为了更好的理解:
For a better understanding:
推荐答案
我正在尝试检索一些文档,但是我需要通过一些数据("ListIDX")对它们进行排序
I am trying to retrieve some documents but I need them to be ordered by some data ("ListIDX")
下面的代码行肯定会帮助您实现这一目标:
The following line of code will definitely help you achieve that:
db.collection("users").document(userID).collection("wishlists").order(by: "ListIDX").getDocuments() {/* ... */}
不允许在.order(by: "ListIDX")
之后添加另一个.document(list.name)
调用,因为此函数返回一个Firestore Query
对象,并且由于该类中不存在该函数,因此无法链接该函数.
Adding another .document(list.name)
call after .order(by: "ListIDX")
is not allowed because this function returns a Firestore Query
object and there is no way you can chain such a function since it does not exist in that class.
此外,Firestore查询很浅,这意味着它们仅从查询所针对的集合中获取项目.无法通过单个查询从顶级集合和子集合中获取文档. Firestore不一次性支持跨不同集合的查询.单个查询只能使用单个集合中的文档属性.因此,我能想到的最简单的解决方案是使用两个不同的查询并在客户端合并结果.第一个查询是上面的查询,它返回愿望清单"列表,第二个查询是一个查询,可以帮助您获取存在于wünsche
子集合中每个wishlist
对象中的所有愿望.
Furthermore, Firestore queries are shallow, meaning that they only get items from the collection that the query is run against. There is no way to get documents from a top-level collection and a sub-collection in a single query. Firestore doesn't support queries across different collections in one go. A single query may only use the properties of documents in a single collection. So the most simple solution I can think of would be to use two different queries and merge the results client-side. The first one would be the above query which returns a list of "wishlists" and the second one would be a query that can help you get all wishes that exist within each wishlist
object in wünsche
subcollection.
这篇关于Swift-Firebase-订单收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!