【发布时间】:2019-04-03 12:22:49
【问题描述】:
我有以下代码结构,如何在后台线程上运行这段代码,并在 FIFO 中串行执行所有方法。
如何等待函数执行完它的所有语句然后移动到下一个函数?
func downloadImagesAndProcess(){
// i need these methods to execute one by one i.e when saveimages completes fully only then call resizeimages
saveImages()
resizeImages()
shareImgs()
}
func saveImages(){
// long async tasks
for (index, image) in (self.images.enumerated())! {
KingfisherManager.shared.retrieveImage(with: URL(string:image.imageFile)!) { result in
switch result {
case .success(let value):
self.saveImageDocumentDirectory(image: value.image, imageName: imgNameStr)
case .failure(let error):
print(error) // The error happens
}
}
}
}
func resizeImages(){
// long running tasks
}
func shareimgs(){
//share
}
我需要这些方法一一执行,即当saveImages完全完成时才调用resizeImages
如何等待函数执行完它的所有语句然后移动到下一个函数?
【问题讨论】:
-
你可以使用串行队列。
-
@jarvis12 串行队列不执行函数内的所有语句..它移动到其他函数
-
对不起,没有任何实际代码很难帮助你;可能有不同的解决方案,您尝试过吗?
-
@RicoCrescenzio 我已经尝试过调度组和串行队列,但它们对我不起作用
-
放上你正在使用的代码,不仅仅是方法声明
标签: ios swift concurrency grand-central-dispatch