A thread safe, type safe, and memory safe Promises/A+ implementation for Swift 5
Here are some examples pulled directly from the included Demo code.
Building a promise that returns the result of a network request:
import Foundation
import FranticApparatus
public struct NetworkResult {
public let response: URLResponse
public let data: Data
}
public enum NetworkError : Error {
case unexpectedData(Data)
case unexpectedResponse(URLResponse)
case unexpectedStatusCode(Int)
case unexpectedContentType(String)
}
public protocol NetworkLayer : class {
func requestData(_ request: URLRequest) -> Promise<NetworkResult>
}
public final class SimpleURLSessionNetworkLayer : NetworkLayer {
private let session: URLSession
public init() {
let sessionConfiguration = URLSessionConfiguration.default
self.session = URLSession(configuration: sessionConfiguration)
}
deinit {
session.invalidateAndCancel()
}
public func requestData(_ request: URLRequest) -> Promise<NetworkResult> {
return Promise<NetworkResult> { (fulfill, reject) in
session.dataTask(with: request, completionHandler: { (data, response, error) in
if let error = error {
reject(error)
}
else if let data = data, let response = response {
fulfill(NetworkResult(response: response, data: data))
}
else {
fatalError("Unexpected")
}
}).resume()
}
}
}
Chaining off of a network request promise to display thumbnails in a collection view:
FranticApparatus
A thread safe, type safe, and memory safe Promises/A+ implementation for Swift 5
Here are some examples pulled directly from the included Demo code.
Building a promise that returns the result of a network request:
Chaining off of a network request promise to display thumbnails in a collection view:
Loading JSON, parsing it, and then waiting for all thumbnails to load before displaying them:
Contact
Justin Kolb
@nabobnick
License
FranticApparatus is available under the MIT license. See the LICENSE file for more info.