duemunk / Async

Syntactic sugar in Swift for asynchronous dispatches in Grand Central Dispatch
MIT License
4.59k stars 316 forks source link

This is my upload code, how can i use async #98

Closed huangboju closed 8 years ago

huangboju commented 8 years ago
2016-09-02 10 50 28
YMonnier commented 8 years ago

Hello,

try something like that

func upload() {
  if originalImages.isEmpty {
    showAppFailureMessage("...")
    return
  }

  navigationItem.showTitleView(".")
  let uploadGroup = Async.Group()
  urls = Array(count: originalImages.count, repeatedValue: "")
  for (i, image) in originalImages.enumerate() {
    if let image = image {
      if let imageData = UIImageJPEGRepresentation(image, 0.6) {
        uploadGroup.enter()
        Http.uploadToCloud("oss", filename: "...", data: imageData, controller: self; success: { [weak self] (urlSuffix) in 
          uploadGroup.leave()
          let imageUrl = "(urlSuffix)"
          self?urls[i] = imageUrl
        }, failure: { [weak self] in 
          self?.showAppFailureMessage("...")
        })
      }
    }
  }
  uploadGroup.main {
    self.navigationItem.hideTitleView()
    self.projection
  }
}
huangboju commented 8 years ago

You're not right,

func upload() {
  if originalImages.isEmpty {
    showAppFailureMessage("...")
    return
  }

  navigationItem.showTitleView(".")
  let uploadGroup = Async.Group()
  urls = Array(count: originalImages.count, repeatedValue: "")
  for (i, image) in originalImages.enumerate() {
    if let image = image {
      if let imageData = UIImageJPEGRepresentation(image, 0.6) {
        uploadGroup.enter()
// 1
        Http.uploadToCloud("oss", filename: "...", data: imageData, controller: self; success: { [weak self] (urlSuffix) in 
// 3 I want to here is step 2
          uploadGroup.leave()
          let imageUrl = "(urlSuffix)"
          self?urls[i] = imageUrl
        }, failure: { [weak self] in 
          self?.showAppFailureMessage("...")
        })
      }
    }
  }
  uploadGroup.main {
// 2 I want to here is step 3
    self.navigationItem.hideTitleView()
    self.projection
  }
}