簡體   English   中英

我無法獲取數組並從 Firebase 存儲中放入另一個數組 swift

[英]I can't get array and put in the another array swift from Firebase storage

我想從 Firebase 存儲中獲取文件夾和圖像。 在這段代碼上,除了片刻之外,所有的工作。 我不能在數組 self.collectionImagesArray 中使用 append 數組 self.collectionImages。 我沒有錯誤,但數組 self.collectionImagesArray 是空的

class CollectionViewModel: ObservableObject {
@Published var collectionImagesArray: [[String]] = [[]]
@Published var collectionImages = [""]

init() {
  
   var db = Firestore.firestore()
   let storageRef = Storage.storage().reference().child("img")

   storageRef.listAll { (result, error) in
        if error != nil {
            print((error?.localizedDescription)!)
        }

        for prefixName in result.prefixes {
            let storageLocation = String(describing: prefixName)
            
            let storageRefImg = Storage.storage().reference(forURL: storageLocation)
        
            storageRefImg.listAll { (result, error) in
                if error != nil {
                    print((error?.localizedDescription)!)
                }
                
                for item in result.items {
                    // List storage reference
                    let storageLocation = String(describing: item)
                    let gsReference = Storage.storage().reference(forURL: storageLocation)
                    
                    // Fetch the download URL
                    gsReference.downloadURL { url, error in
                      if let error = error {
                        // Handle any errors
                        print(error)
                      } else {
                        // Get the download URL for each item storage location
                          let img = "\(url?.absoluteString ?? "placeholder")"
                          self.collectionImages.append(img)
                          print("\(self.collectionImages)")
                        }
                      }
                    }
                self.collectionImagesArray.append(self.collectionImages)
                print("\(self.collectionImagesArray)")
                }
            //
            self.collectionImagesArray.append(self.collectionImages)
        }
    }
}

如果我將 self.collectionImagesArray.append(self.collectionImages) 關閉它的作品,但它不是我想要的

問題是由於調用downloadURL是一個異步操作,因為它需要調用服務器。 在進行該調用時,您的主要代碼會繼續運行,以便用戶可以繼續使用該應用程序。 然后,當服務器返回一個值時,將調用您的閉包/完成處理程序,將 URL 添加到數組中。 所以你的print("\(self.collectionImagesArray)")發生在self.collectionImages.append(img)被調用之前。

您還可以按照打印語句在 output 中出現的順序看到這一點。您將首先看到完整的空數組,然后才看到print("\(self.collectionImages)")輸出。

此問題的解決方案始終相同:您需要確保僅在所有 URL 都已添加到數組后才使用該數組。 有很多方法可以做到這一點,但一個簡單的方法是檢查您的 URL 數組是否與回調中的result.items長度相同:

...
self.collectionImages.append(img)
if self.collectionImages.count == result.items.count {
    self.collectionImagesArray.append(self.collectionImages)
    print("\(self.collectionImagesArray)")
}

另見:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM