我现在有一个很奇怪的问题,我不知道为什么会这样。
首先,我从Cloud-FirestoreDB中检索了一些documents
。效果很好。
func retrieveUserDataFromDB() -> Void {
// local mutable "WishList" var
var wList: [Wish] = [Wish]()
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.wishListImagesArray.append(UIImage(named: "iconRoundedImage")!)
self.wishListTitlesArray.append(listName as! String)
// 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.wishListTitlesArray.append(listName as! String)
self.wishListImagesArray.append(self.images[listImageIDX as! Int])
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()
}
}
}
// un-hide the collection view
self.theCollectionView.isHidden = false
getWishes()
}
正如您在上面的函数中看到的,我也.append
我的wishListTitlesArray
。但是,如果我尝试访问其中的数据,则不会发生任何事情。
func getWishes (){
let db = Firestore.firestore()
let userID = Auth.auth().currentUser!.uid
var counter = 0
print("yikes")
for list in self.wishListTitlesArray {
print("yeet")
db.collection("users").document(userID).collection("wishlists").document(list).collection("wünsche").getDocuments() { ( querySnapshot, error) in
if let error = error {
print(error.localizedDescription)
}else{
var wList = self.userWishListData[counter]
for document in querySnapshot!.documents {
let documentData = document.data()
let wishName = documentData["name"]
wList.append(Wish(withWishName: wishName as! String, checked: false))
counter += 1
print("hi")
print(wishName as! String)
}
}
}
}
}
现在它正在打印print("yikes")
,因此它甚至都没有进入for-loop
内。
我还尝试在func retrieveUserDataFromDB
的末尾打印所有元素,但这也不起作用。它没有崩溃或没有显示任何错误。
可能只是一个愚蠢的错误,但是如果有人知道为什么会发生这种情况,我将非常感激!
答案 0 :(得分:1)
我不知道Firestore对象,但我认为您的getDocument方法是异步函数。 因此,在执行时间轴中,在调用getDocuments的同时调用了getWishes ... 完成getDocument后,应执行功能getWishes。
要做到这一点,您可以这样移动代码:
func retrieveUserDataFromDB() -> Void {
// local mutable "WishList" var
var wList: [Wish] = [Wish]()
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.wishListImagesArray.append(UIImage(named: "iconRoundedImage")!)
self.wishListTitlesArray.append(listName as! String)
// 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.wishListTitlesArray.append(listName as! String)
self.wishListImagesArray.append(self.images[listImageIDX as! Int])
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()
}
getWishes()
}
}
// un-hide the collection view
self.theCollectionView.isHidden = false
}
检查快速关闭 https://docs.swift.org/swift-book/LanguageGuide/Closures.html
您还可以选中DispatchGroup
https://developer.apple.com/documentation/dispatch/dispatchgroup