Brings the power of promises/futures to iOS development in the form of Tasks
Brings the power of promises/futures to iOS development in the form of Tasks
You'll first need install CocoaPods:
$ gem install cocoapods
Now you'll need to set up CocoaPods for your existing xcode project. Change the working directory to your xcode project's root directory and then:
$ touch Podfile
Then open the
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
pod 'AsyncKit'
Then run the following command, which will install the AsyncKit library into your existing xcode project
$ pod install
Another integration option is to manually add ConstraintKit to your xcode project, by cloning from github and dragging/dropping into your xcode project:
$ git clone ...
AsyncKit brings the power of promises/futures to iOS development in the form of
To build a responsive iOS application, you would need to run network and long operations asynchronously within a background thread to prevent it from slowing the main (UI) thread down.
There are only two core classes:
Note | ||
---|---|---|
If asynchronous operation was successful | ||
If asynchronous operation failed with a |
||
If asynchronous operation failed with a |
...
func fetchDataAsync() -> Task<NSData> {
let manager = TaskManager<NSData>()
dispatch_async(backgroundQueue) {
// Fetches data
manager.complete(data)
}
return manager.task
}
...
fetchDataAsync().then { data in
// Handle data here
}
...
The parameter
The
The
There are two types of
...
func fetchDataAsync() -> Task<NSData> {
let manager = TaskManager<NSData>()
dispatch_async(backgroundQueue) {
let (data, error) = fetchData()
guard let ret = data where error == nil else {
manager.completeWithError(error)
return
}
manager.complete(ret)
}
return manager.task
}
...
fetchDataAsync().then { data in
// Handle data here if successfully fetched data
}.error { (error: NSError) in
// Handle error here if data fetching failed
}
...
activityIndicator.startAnimating()
fetchDataAsync().then { data in
// Handle data
}.error { (error: NSError) in
// Handle error
}.finally {
activityIndicator.stopAnimating()
}
The
AsyncKit also allows you to chain
...
fetchDataAsync().then { data in
// Handle data
return fetchUserAsync()
}.then { user in
// Update user
return saveUserAsync()
}.then { updatedUser in
// Handle updated user
}
...
NOTE
The order of chained
The
Details | |
---|---|
The thread in which the |
|
The main thread A.K.A. |
|
The default priority global thread A.K.A. |
|
Custom thread provided |
// The then block will run in the main thread (where UI related operations should be ran)
fetchDataAsync().then(.Main) {
let alertController = UIAlertController(title: "Completed", message: "Fetched Data Successfully", preferredStyle: .Alert)
let okAction = UIAlertAction(title: "OK", style: .Cancel) { action in
// ...
}
alertController.addAction(okAction)
self.presentViewController(alertController, animated: true) {
// ...
}
}
AsyncKit also allows you to run multiple
Task.join(fetchAndLoadTableAsync(), fetchAndLoadImagesAsync()).then {
// This block executes only if all the tasks sent to the Task.join function complete successfully
}
AsyncKit also provides you with a
...
enum LoginError: ErrorType {
case InvalidCredentials
}
task { () throws in
// The contents of this block will be executed asynchronously in a default priority global queue
let success = self.login(username, password)
if (!success) {
throw LoginError.InvalidCredentials
}
var ret: AnyObject? = nil
// Fetch other data...
return ret
}.then { ret in
// Handle result of the task wrapped code
}.error { (error: ErrorType) in
// Handle ErrorType (in this case, InvalidCredentials)
}
...