AHFuture is a concepts of asynchronous api using idea of futures.
AHFuture implements proven functional concepts in Swift to provide a powerful alternative to completion blocks and support typesafe error handling in asynchronous code.
We write a lot of asynchronous code. Whether we're waiting for something to come in from the network or want to perform an expensive calculation off the main thread and then update the UI, we often do the 'fire and callback' dance. Here's a typical snippet of asynchronous code:
User.logIn(username, password) { user, error in
if !error {
Posts.fetchPosts(user, success: { posts in
// do something with the user's posts
}, failure: handleError)
} else {
handleError(error) // handeError is a custom function to handle errors
}
}Now let's see what BrightFutures can do for you:
User.logIn(username, password).flatMap { user in
Posts.fetchPosts(user)
}.onSuccess { posts in
// do something with the user's posts
}.onFailure { error in
// either logging in or fetching posts failed
}.execute()If success will transfrom User response to UserViewModel that can be used in success block
User.logIn(username, password).map { UserViewModel.init }
.onSuccess { viewModel in
// do something with viewModel
}.onFailure { error in
// either logging in or fetching posts failed
}.execute()If logIn succeed the response will be tranfromed to another AHFuture and the success block will contain result from the second AHFuture
User.logIn(username, password).flatMap { Posts.fetchPosts }
.onSuccess { posts in
// do something with the user's posts
}.onFailure { error in
// either logging in or fetching posts failed
}.execute()Will execute completion if predicate is true.
User.logIn(username, password).flatMap { Posts.fetchPosts }
.filter { PostFilter.isTodaysPost }
.onSuccess { posts in
// do something with the user's posts
}.onFailure { error in
// either logging in or fetching posts failed
}.execute()Retry operator will try to execute AHFuture if there is an error. The parametr shows the number of attempts
User.logIn(username, password).flatMap { Posts.fetchPosts }
.retry(5)
.onSuccess { posts in
// do something with the user's posts
}.onFailure { error in
// either logging in or fetching posts failed
}.execute()Recover operator helps transform error to a placeholder object.
User.logIn(username, password).flatMap { Posts.fetchPosts }
.retry(5)
.recover {ErrorHandler.transformToPlaceholderModel}
.onSuccess { posts in
// do something with the user's posts
}.onFailure { error in
// either logging in or fetching posts failed
}.execute()Sometimes we need to run our work on a particular thread and observe on another. For this purposes there is a run operator
Calculator.primeNumber(number)
.run(on: myGlobalQueue)
.observe(on: .main)
.onSuccess { posts in
// do something with the user's posts
}.onFailure { error in
// either logging in or fetching posts failed
}.execute()AHFuture is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "AHFuture"- add timeout operator
- add synchronize/concat/zip operators
Alex Hmelevski, alexei.hmelevski@gmail.com
AHFuture is available under the MIT license. See the LICENSE file for more info.