【发布时间】:2020-01-01 22:11:39
【问题描述】:
我正在尝试检索一些文档,但我需要按“愿望清单”中的某些数据(“ListIDX”)对它们进行排序 - collection。
我试过了,但这是不允许的:
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
}
}
}
}
这是我最终真正想要实现的目标:
self.dataSourceArray[ListIDX].wishData = wList
更新
我还有一个函数可以按正确的顺序检索我的wishlists。也许我可以在那里添加getWishes,这样它的顺序也是正确的。
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()
}
为了更好地理解:
【问题讨论】:
-
查看您建议的代码:collection("wishlists").order(by: "ListIDX").document(list.name) - 您的“wishlists”集合中可能有多个文档,但是您只获取具有 id = list.name 的一个。所以订购这些文件没有任何意义。这就是它不被支持的原因。您可能应该更好地解释您的数据结构是什么以及您想要实现什么。
-
@alrgrid 我需要在 sourceDataArray 的正确索引处插入 wList。那是我的问题。如果 list.name 已排序,则检索 wünsche 将是正确的顺序。你知道我的意思吗?
-
请不要重复问同一个问题。它将信息分布在多个帖子中。一次只关注一个问题,帮助我们帮助您。另外两个问题的cmets中提到,Firestore是异步的,数据可能在不同的时间从不同的节点返回。唯一的保证是数据是否在查询中排序 - 例如,您的
listIDX数据将按此排序。解决方案是在检索到所有数据后,通过您想要的任何属性简单地对self.dataSourceArray进行排序。 -
另一个选项,如果你想向 dataSource 数组添加额外的信息是在该数组中查找列表对象的索引,然后添加额外的数据。类似这个伪代码
let index = self.dataSourceArray.firstIndex(of: list),然后使用该索引来更新该索引处的对象。
标签: ios swift firebase google-cloud-firestore