ios - 在 Swift 中更新变量之前数组正在更新

标签 ios arrays swift google-cloud-firestore swiftui

我正在尝试从 Firestore 获取玩具列表并将其放入数组中 但是当我调用函数时,它返回空数组,并且在返回后就打印 Toy 对象,因此顺序被破坏了。

我认为闭包对我有帮助,但我想我不知道如何使用它们,而且来自 Google 的示例对我没有帮助

这是我的代码(我使用 SwiftUI,所以我创建了带变量的 swift 文件)

let db = Firestore.firestore()
class DataLoade {
    func loadFirebase(completionHandler: @escaping (_ toys: [Toy]) -> ()){
        var toysar: [Toy] = []
        let toysRef = db.collection("Toys")
        toysRef.getDocuments() { (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
            } else {
                for document in querySnapshot!.documents {
                    var name: String = document.get("name") as! String
                    var id: Int = document.get("id") as! Int
                    var description: String = document.get("description") as! String
                    var imageName: String = document.get("imageName") as! String
                    var price: String = document.get("price") as! String
                    var category: String = document.get("category") as! String
                    var timeToy = Toy(id: id, name: name, imageName: imageName, category: category, description: description, price: price)
                    toysar.append(timeToy)


                }




            }
        }

        completionHandler(toysar)
    //    print(toysar)

    }




}


这就是它打印出来的内容:

[] // it prints empty array, but it  is in the end of the code
Toy(id: 1001, name: "Pikachu", imageName: "pikachu-plush", category: "lol", description: "kek", price: "350₽") // and now it prints Toy object, however it is in the start of the code 

好的,所以我尝试为我的函数创建完成处理程序,就像在“重复”答案中一样,但这不起作用:数组在完成处理程序工作之前返回

ContentView.swift  
func updateArray() -> [Toy]{
    dl.loadFirebase() { toys in
            ll = toys

            }
    print("lol \(datas)") // prints «lol []»
    return ll
}

最佳答案

您可以使用 DispatchGroup 等待异步任务。但诀窍是不要将异步任务与 return 语句相关联。相反,使用 closures 在任务完成后执行操作。 免责声明:我在 SO 上写了这篇文章,我提前为语法问题道歉。

let toyData = loadFirebase( { (toys) in
    print(toys)
    //Do something with toys when done
    //You could add another completionHandler incase it fails. 
    //So 1 for pass and 1 for fail and maybe another for cancel. W/e u want
} )
let db = Firestore.firestore()

func loadFirebase(completionHandler:@escaping ((toys: [Toy]?) -> Void)) {
    //Create Group
    let downloadGroup = DispatchGroup()
    var toysar: [Toy] = []
    let toysRef = db.collection("Toys")
    //If you had multiple items and wanted to wait for each, just do an enter on each.
    downloadGroup.enter()
    toysRef.getDocuments() { (querySnapshot, err) in
        if let err = err {
            print("Error getting documents: \(err)")
        } else {
            for document in querySnapshot!.documents {
                var name: String = document.get("name") as! String
                var id: Int = document.get("id") as! Int
                var description: String = document.get("description") as! String
                var imageName: String = document.get("imageName") as! String
                var price: String = document.get("price") as! String
                var category: String = document.get("category") as! String
                var timeToy = Toy(id: id, name: name, imageName: imageName, category: category, description: description, price: price)
                toysar.append(timeToy)
                print(timeToy)
            }
        //We aren't done until AFTER the for loop, i.e., each item is grabbed.
        downloadGroup.leave()
        }
    }
    //Once the queue is empty, we notify the queue we are done
    downloadGroup.notify(queue: DispatchQueue.main) {
        completionHandler(toys)
    }
}
import SwiftUI
var dl = DataLoade()
var ll: [Toy] = []

let semaphore = DispatchSemaphore(value: 1)
struct ContentView: View {
    var items: [Toy]
    var body: some View {

        NavigationView{
        ScrollView(){
            VStack(alignment: .leading){
        ToyRow(category: "Наш выбор", toys: items)

            Spacer()
            ToyRow(category: "Акции", toys: items)


            }
            }.navigationBarTitle(Text("Игрушки г.Остров"))}


    }
}
func upe(completionHandler:@escaping ((toys: [Toy]?){
    dl.loadFirebase(completionHandler: { toy in
        ll.append(contentsOf: toy!)
        completionHandler(ll)
    } )
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        upe(completionHandler: { (toys) in 
            DispatchQueue.main.async {
                ContentView(items: toys)
            }
        })
    }
}

关于ios - 在 Swift 中更新变量之前数组正在更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58650808/

相关文章:

swift - 带有流氓 .o 文件的无效 Bundle 结构

ios - 使用 Alamofire Swift 4 拉取刷新

android - 初创公司首先要使用 iOS 或 Android 应用程序

c - 使用 malloc() 分配内存时出错,只能向表中添加一项

ios - 使用 Swift 在键盘上启动一个带有搜索按钮的新 View Controller

arrays - Swift 闭包数组 - 查找元素

ios - 如何使某些协议(protocol)方法仅适用于特定的 iOS 版本?

iphone - 如何仅在UITextView上强制水平滚动

在 C 中从 char 数组创建字符串

arrays - 在C中交换2个数组