gpt4 book ai didi

ios - Swift - Firebase - 订单集合

转载 作者:行者123 更新时间:2023-11-29 05:12:16 24 4
gpt4 key购买 nike

我正在尝试检索一些文档,但我需要按照我的“愿望 list ”-集合中的一些数据(“ListIDX”)对它们进行排序。

我尝试了这个,但这是不允许的:

db.collection("users").document(userID).collection("wishlists").order(by: "ListIDX").document(list.name).collection("wünsche"). getDocuments()

这是我的函数:

    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

更新

我还有一个函数可以按正确的顺序检索我的愿望 list 。也许我可以在其中添加 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()
}

为了更好地理解:

git repo

最佳答案

正如 @algrid 所说,如果您要使用 list.name 获取特定元素,则使用 order()collection 进行排序是没有意义的 在最后,而不是第一个或最后一个。我建议将您的代码更改为:

db.collection("users").document(userID).collection("wishlists").document(list.name).collection("wünsche").getDocuments()

关于ios - Swift - Firebase - 订单集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59566131/

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