AsynchronousSwift provides many tools for efficient asynchronous work based on Grand Central Dispatch.
Unlike other asynchronous libraries, AsynchronousSwift attempt to optimize the usage of threads to limit the number of context switch.
At the moment we can find different tools:
- ThreadSafe
- Future & Promise
- Async & Await
let threadSafeArray = ThreadSafe<[Int]>([0])
let value = threadSafeArray.value // Safe only with Duplicable
threadSafeArray.value = [1]
threadSafeArray <- [1]
threadSafeArray.onChange(on: .global(qos: .background)) { newValue in
print("New Array : \(newValue)")
}
let count = threadSafeArray.readOnly.sync { value in value.count }
print("Array size : \(count)")
threadSafeArray.readOnly.async { value in
print("Array size : \(value.count)")
}
let element = threadSafeArray.readAndWrite.sync { value in value.remove(at: 0) }
print("First element was : \(element)")
threadSafeArray.readAndWrite.async { value in
print("First element was : \(value.remove(at: 0))")
}
let promise = Promise<Int>()
let future = promise.reject(with: NSError(domain: "", code: 0))
let future = promise.resolve { 1 }
let future = promise.future
future.then { value in /* code */ }
.then(qos: .background) { value in /* code */ }
.catch{ error in /* code */ }
.finally { /* code */ }
let result = future.sync { value in return /* code */ }
future.isFulfilled
future.isPending
future.isRejected
let future = async { return /* code */ }
result = try? await(future)
AsynchronousSwift is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'AsynchronousSwift'
AsynchronousSwift is available under the MIT license. See the LICENSE file for more info.