SwiftDeferred 1.0.4

SwiftDeferred 1.0.4

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布上次发布2017年8月
SwiftSwift版本3.0
SPM支持SPM

Damouse维护。



  • 作者
  • Mickey Barboi

Deferred

这是一个受Python Twisted和Promise/A+强烈启发的Swift时的Deferred实现。它还受到了ReactiveCocoa在管理值转换和Pipeline方面的影响。

该项目是为Swift 1.3编写的,尚未针对Swift 3进行更新。我现在没有更新它的计划。

基本上,Deferred通过将处理器函数链接到一起、转换或序列化数据以及管理错误来编排处理器函数的调用。它的主要重点是使异步代码(尤其是网络代码)更容易处理,但不依赖于任何特定的网络库实现。

示例

import Deferred

// Create a new deferred
var d = Deferred<Void>()

// Build the chain. Values are passed down and transformed based on parameter types
// The next deferreds in the chain do not require signatures, their type is inferred from the previous 
// deferred's return type
d.chain { () -> Deferred<String> in
    print(1)
    return f
}.then { s in
    print(s)
    print(2)
}.then {
    print(3) // I dont take any args, since the block above me didnt reutn a deferred
}.error { err in
    print("Error: \(err)")
}

// Fire the callback 
d.callback([])

这是一个用于处理异步操作库。以下是一个更具象的与AF的示例

  Alamofire.request(.GET, url).json("title") { (post: String) -> () in
      // Loaded a single post. Request the rest of the article
      return Alamofire.request(.GET, url).json("article)
  }.chain { (article: String) -> () in
    // Loaded the article. Return it as an array of words
    return article.split(' ')
  }.then { words in
    print("Have words!")
  }.error { e in
      print("Error occured: \(e)")
  }

进度和功能

功能列表

  • 自动转换
  • 允许nil通过的可选转换
  • 基本的Deferred转换
  • AnyFunction泛型接收者和工厂方法
  • 模型(作为Silver部分实现)