Skip to content

chain Task.all more than once, last then not called.  #59

@taka0125

Description

@taka0125

Under the following conditions, last then not called.

  • Task.all chain 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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions