Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
taka0125 opened this issue Mar 18, 2016 · 3 comments
Closed

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

taka0125 opened this issue Mar 18, 2016 · 3 comments

Comments

@taka0125
Copy link

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")])
@inamiy
Copy link
Member

inamiy commented Mar 19, 2016

Thanks for the catch!
What is happening in your code is Task.all([] /* empty */).then { /* this will never be called in ver 4.2.0 */ }.
I have never assumed tasks-argument to be empty, so fulfilling with empty array should solve this issue. I will address that in a moment.

@inamiy
Copy link
Member

inamiy commented Mar 19, 2016

Done in #60.

@inamiy inamiy closed this as completed Mar 19, 2016
@taka0125
Copy link
Author

👍

Thank you for your quick response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants