-
Notifications
You must be signed in to change notification settings - Fork 176
Closed
Description
Under the following conditions, last then not called.
Task.allchain more than once- all tasks is empty
what would be a good way to do it?
Sample code
final class Image {
let url: String
var downloaded: Bool = false
var converted: Bool = false
typealias DownloadTask = Task<NSProgress, String, NSError?>
typealias ConvertTask = Task<NSProgress, String, NSError?>
init(url: String) {
self.url = url
}
func download() -> DownloadTask {
return DownloadTask { progress, fulfill, reject, configure in
// download image and cache
self.downloaded = true
return fulfill("path to image")
}
}
func convert() -> ConvertTask {
return ConvertTask { progress, fulfill, reject, configure in
// convert image
self.converted = true
return fulfill("path to converted image")
}
}
}
func downloadAndConvertAll() {
let images = [
Image(url: "http://example.com/1.jpg"),
Image(url: "http://example.com/2.jpg"),
Image(url: "http://example.com/3.jpg"),
]
// I want to exclude downloaded image
let downloadTasks = images.filter { !$0.downloaded }.map { $0.download() }
// downloadTasks.count == 0 and convertTasks.count == 0 => doSomething not called
Task.all(downloadTasks).then { _ -> Task<Image.ConvertTask.BulkProgress, [String], NSError?> in
// I want to exclude converted image
let convertTasks = images.filter { !$0.converted }.map { $0.convert() }
return Task.all(convertTasks)
}.then { _ in
doSomething()
}
}
func doSomething() {
print("doSomething")
}found workaround
Task.all(downloadTasks.count > 0 ? downloadTasks : [Image.DownloadTask(value: "dummy")])and
Task.all(convertTasks.count > 0 ? convertTasks : [Image.ConvertTask(value: "dummy")])Metadata
Metadata
Assignees
Labels
No labels