gpt4 book ai didi

ios - Firestore 使用 for 循环查询多个文档

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

我正在尝试使用for循环查询多个文档。

我的数据库设置如下所示:

用户 -> 愿望 list -> 所有用户愿望 list (包含具有 name 的不同愿望 list ) -> wünsche

正在检索项目,但顺序错误。我尝试了几种不同的方法,但到目前为止没有任何效果。

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
}

for list in self.dataSourceArray {
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{
// create 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))
}

self.dataSourceArray[counter].wishData = wList
counter += 1
}
}
}
}

我在另一个函数中调用此函数,该函数以正确的顺序检索所有愿望 list :

func getWishlists() {
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])
}

// reload collectionView and tableView
self.theCollectionView.reloadData()
self.dropDownButton.dropView.tableView.reloadData()

}
}
self.theCollectionView.isHidden = false
self.getWishes()
}
}

*DataSourceArray 按正确顺序排列:* 主要愿望 list 、目标、提升

第二个打印的输出 - 测试:提升、目标、主要愿望 list

最佳答案

似乎您试图同时进行一堆 API 调用,但它在不同时间返回值。您可以尝试同步进行调用以维持顺序,或者您可以尝试使用调度组,如下面的伪代码:

let myGroup = DispatchGroup()

struct DataItem {
let order: Int
let data: DataYouWantToSave
}

var fetchedData = [DataItem]()

for i in list {
myGroup.enter()

let dataItem = DataItem()
dataItem.order = i

db.collection...
print("Finished request \(i)")
dataItem.data = DataYouWantToSave
fetchedData.apped(dataItem)
myGroup.leave()
}
}

myGroup.notify(queue: .main) {
print("Finished all requests.")
// Reorder your array of data items here.
let sortedArray = fetchedData.sorted(by: { $0.order > $1.order })

// If you just want the array of data values
let newData: [DataYouWantToSave] = sortedArray.map { $0.data }
}

关于ios - Firestore 使用 for 循环查询多个文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59569139/

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