TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Sep 2016 |
SPMSupports SPM | ✗ |
Maintained by Victor Pavlychko.
Most task/operation manadgement libraries either build task infrastructure from scratch or focus on defining custom Operation
subclasses.
SwiftyTask aims to build workflows on top of existing Operation
infrastructure extending it with result/error handling. Any third-parrty Operation
subclasses can be easily extended to support TaskProtocol
and participate in complex workflows.
SwiftyTasks defines following task-related protocols:
AnyTask
: provides list of backing tasks, is adopted by Operation
classTaskProtocol
: extenda AnyTask
adding access to execution resultLibrary also defines following task-related classes:
Task<ResultType>
: base class, conforms to TaskProtocol
, provides result handlingBlockTask<ResultType>
: subclass to wrap any code block into a Task
AsyncTask<ResultType>
: extenda Task
class with asynchronous Operation
state handlingAsyncBlockTask<ResultType>
: subclass to wrap any asynchronous code block into an AsyncTask
AdapterTask<ResultType>
: subclass wrapping list on operations custom result blockFollowing operators are defined to manage tasks:
+=
: enqueue task in OperationQueue<~
: performs partial function application where operand on the right may be either value or task~>
: performs deferred function call given value/task list on the right~~
: adds task on the right as a dependency to task on the leftTask
workflows circulate around partial application and functions returning Task
s.
Use <~
operator to perform partial application. Function on the left will be wrapped into a task scheduled to be executed after value on the right is ready.
Use ~>
operator to convert list of values. Function on the right will be wrapped into a task scheduled to be executed when all values on the left are ready.
Finally +=
operator is overloaded to enqueue tasks into OperationQueue
Following is an example of real-world use to create video, save it to photo library and put into a gallery folder.
let createVideoOperation = OperationQueue.main
+= CreateVideoOperation()
let createVideoAssetOperation = OperationQueue.main
+= CreateVideoAssetOperation.init(videoURL:)
<~ createVideoOperation
let createAssetCollectionOperation = OperationQueue.main
+= CreateAssetCollectionOperation.init(title:)
<~ "Convenience"
let addAssetToCollectionOperation = OperationQueue.main
+= AddAssetToCollectionOperation.init(asset:collection:)
<~ createVideoAssetOperation
<~ createAssetCollectionOperation
addAssetToCollectionOperation.completionBlock = {
completionBlock()
}
SwiftyTasks is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "SwiftyTasks"
Victor Pavlychko, [email protected]
SwiftyTasks is available under the MIT license. See the LICENSE file for more info.