Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift:关闭的执行顺序_Swift - Fatal编程技术网

Swift:关闭的执行顺序

Swift:关闭的执行顺序,swift,Swift,我有以下功能。 当我运行这个程序时,“hit2222”首先打印,“hit4444”第二次打印,“图像已下载”最后打印 既然打印(“hit2222”)处于关闭状态,并且getImages只会在下载完所有图像后执行,那么“图像已下载”不应该先打印吗 func updateCurrentUser() { getImages(completion: { downloadedImages in print("hit2222") }) print("hit

我有以下功能。 当我运行这个程序时,“hit2222”首先打印,“hit4444”第二次打印,“图像已下载”最后打印

既然打印(“hit2222”)处于关闭状态,并且getImages只会在下载完所有图像后执行,那么“图像已下载”不应该先打印吗

func updateCurrentUser() {

    getImages(completion: { downloadedImages in
            print("hit2222")
    })

    print("hit444")
    self.currentUser.user.images = [UIImage(systemName: "star"), nil, nil, nil, nil, nil, nil, nil, nil]

}

func getImageFromStorage(imagePath: String, completion: @escaping(UIImage?) -> Void) {
    // downloads one image from our database

    let storageRef = Storage.storage().reference()
    let pathReference = storageRef.child(imagePath)

    // Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes)
    pathReference.getData(maxSize: 1 * 1024 * 1024) { data, error in
        if error != nil {
            print("error hit : \(error!.localizedDescription)")
        } else {
            let myImage = UIImage(data: data!)
            print("Image downloaded successfully")
            completion(myImage)
        }
    }
}

func getImages(completion: @escaping([UIImage?]) -> Void) {
    // downloads multiple images by calling getImageFromStorage multiple times

    var downloadedImages: [UIImage?] = []
    for i in 0...8 {
        let imagePath = "/img" + String(i)
        getImageFromStorage(imagePath: imagePath, completion: { myImage in
            downloadedImages.append(myImage)
        })
    }
    completion(downloadedImages)
}

如果您有多个异步请求,并且需要等待所有请求完成,则可以使用
DispatchGroup
在组中的所有任务完成时通知您

在您的情况下,在调用
getImages

func getImages(completion: @escaping([UIImage?]) -> Void) {

   var downloadedImages: [UIImage?] = []

   let downloadGroup = DispatchGroup()

   for i in 0...8 {

       downloadGroup.enter()

       let imagePath = "/img" + String(i)
       getImageFromStorage(imagePath: imagePath, completion: { myImage in
           downloadedImages.append(myImage)

           downloadGroup.leave()
       })
   }

   downloadGroup.notify(queue: .main) {
       // will be called when all requests are done
       completion(downloadedImages)
   }
}

您的
getImageFromStorage
可能执行了一个异步(比如,使用
URLRequest
)操作(未显示在发布的代码中),因为您说它下载了一些东西。因此,这意味着
getImageFromStorage
会立即返回,而另一个线程会下载该图像。@NewDev我用getImageFromStorage代码更新了帖子。在更新self.currentUser.user.images之前,我对如何重写代码以使getImages()完成执行感到困惑