Jamel  O'Reilly

Jamel O'Reilly

1658123700

Promis: The Easiest Future and Promises Framework in Swift

Promis

The easiest Future and Promises framework in Swift. No magic. No boilerplate.

Overview

While starting from the Objective-C implementation of JustPromises and keeping the code minimalistic, this library adds the following:

  • conversion to Swift 4
  • usage of generics to allow great type inference that wasn't possible in Objective-C
  • overall refactoring for fresh and modern code
  • remove the unnecessary and misleading concept of Progress causing bad patterns to emerge

You can read about the theory behind Future and Promises on Wikipedia, here are the main things you should know to get started.

  • Promises represent the promise that a task will be fulfilled in the future while the future holds the state of such resolution.
  • Futures, when created are in the unresolved state and can be resolved with one of 3 states: with a result, an error, or being cancelled.
  • Futures can be chained, allowing to avoid the pyramid of doom problem, clean up asynchronous code paths and simplify error handling.

Promis brags about being/having:

  • Fully unit-tested and documented 💯
  • Thread-safe 🚦
  • Clean interface 👼
  • Support for chaining ⛓
  • Support for cancellation 🙅‍♂️
  • Queue-based block execution if needed 🚆
  • Result type provided via generics 🚀
  • Keeping the magic to the minimum, leaving the code in a readable state without going off of a tangent with fancy and unnecessary design decisions ಠ_ಠ

Alternatives

Other open-source solutions exist such as:

Promis takes inspiration from the Objective-C version of JustPromises developed by the iOS Team of Just Eat which is really concise and minimalistic, while other libraries are more weighty.

Usage

The following example should outline the main benefits of using futures via chaining.

let request = URLRequest(url: URL(string: "http://example.com")!)

// starts by hitting an API to download data
getData(request: request).thenWithResult { data in
    // continue by parsing the retrieved data
    parse(data: data)
}.thenWithResult { parsedData in
    // continue by mapping the parsed data
    map(data: parsedData)
}.onError { error in
    // executed only in case an error occurred in the chain
    print("error: " + String(describing: error))
}.finally(queue: .main) { future in
    // always executed, no matter the state of the previous future or how the chain did perform
    switch future.state {
        case .result(let value):
            print(String(describing: value))
        case .error(let err):
            print(String(describing: err))
        case .cancelled:
            print("future is in a cancelled state")
        case .unresolved:
            print("this really cannot be if any chaining block is executed")
        }
}

The functions used in the example have the following signatures:

func getData(request: URLRequest) -> Future<Data>
func parse(data: Data) -> Future<[Dictionary<String,AnyObject>]>
func map(data: [Dictionary<String,AnyObject>]) -> Future<[FooBar]>

Promises and Futures are parametrized leveraging the power of the generics, meaning that Swift can infer the type of the result compile type. This was a considerable limitation in the Objective-C world and we can now prevent lots of issues at build time thanks to the static typing nature of the language. The state of the future is an enum defined as follows:

enum FutureState<ResultType> {
    case unresolved
    case result(ResultType)
    case error(Error)
    case cancelled
}

Promises are created and resolved like so:

let promise = Promise<ResultType>()
promise.setResult(value)
// or
promise.setError(error)
// or
promise.cancel()

Continuation methods used for chaining are the following:

func then<NextResultType>(queue: DispatchQueue? = nil, task: @escaping (Future) -> Future<NextResultType>) -> Future<NextResultType>
func thenWithResult<NextResultType>(queue: DispatchQueue? = nil, continuation: @escaping (ResultType) -> Future<NextResultType>) -> Future<NextResultType> {
func onError(queue: DispatchQueue? = nil, continuation: @escaping (Error) -> Void) -> Future {
func finally(queue: DispatchQueue? = nil, block: @escaping (Future<ResultType>) -> Void)

All the functions can accept an optional DispatchQueue used to perform the continuation blocks.

Best practices

Functions wrapping async tasks should follow the below pattern:

func wrappedAsyncTask() -> Future<ResultType> {

    let promise = Promise<Data>()
    someAsyncOperation() { data, error in
        // resolve the promise according to how the async operations did go
        switch (data, error) {
        case (let data?, _):
            promise.setResult(data)
        case (nil, let error?):
            promise.setError(error)
        // etc.
        }
    }
    return promise.future
}

You could chain an onError continuation before returning the future to allow in-line error handling, which I find to be a very handy pattern.

// ...
return promise.future.onError {error in
    // handle/log error
}

Pitfalls

When using then or thenWithResult, the following should be taken in consideration.

...}.thenWithResult { data -> Future<NextResultType> in
    /**
    If a block is not trivial, Swift cannot infer the type of the closure and gives the error
    'Unable to infer complex closure return type; add explicit type to disambiguate'
    so you'll have to add `-> Future<NextResultType> to the block signature
    
    You can make the closure complex just by adding any extra statement (like a print).
    
    All the more reason to structure your code as done in the first given example :)
    */
    print("complex closure")
    return parse(data: data)
}

Please check the GettingStarted playground in the demo app to see the complete implementation of the above examples.

Installation

CocoaPods

Add Promis to your Podfile

use_frameworks!
target 'MyTarget' do
    pod 'Promis', '~> x.y.z'
end
$ pod install

Carthage

github "albertodebortoli/Promis" ~> "x.y.z"

Then on your application target Build Phases settings tab, add a "New Run Script Phase". Create a Run Script with the following content:

/usr/local/bin/carthage copy-frameworks

and add the following paths under "Input Files":

$(SRCROOT)/Carthage/Build/iOS/Promis.framework

Author

Alberto De Bortoli albertodebortoli.website@gmail.com Twitter: @albertodebo GitHub: albertodebortoli website: albertodebortoli.com

License

Promis is available under the Apache 2 license in respect of JustPromises which this library takes inspiration from. See the LICENSE file for more info.

Author: albertodebortoli
Source Code: https://github.com/albertodebortoli/Promis
License: MIT license

#ios #swift 

What is GEEK

Buddha Community

Promis: The Easiest Future and Promises Framework in Swift
Houston  Sipes

Houston Sipes

1600430400

10 Free Online Resources To Learn Swift Language

Swift is a fast and efficient general-purpose programming language that provides real-time feedback and can be seamlessly incorporated into existing Objective-C code. This is why developers are able to write safer, more reliable code while saving time. It aims to be the best language that can be used for various purposes ranging from systems programming to mobile as well as desktop apps and scaling up to cloud services.

Below here, we list down the 10 best online resources to learn Swift language.

(The list is in no particular order)

#developers corner #free online resources to learn swift language #learn swift #learn swift free #learn swift online free #resources to learn swift #swift language #swift programming

Top Swift Development Companies | Top Swift Developers - TopDevelopers.co

A thoroughly researched list of top Swift developers with ratings & reviews to help find the best Swift development companies around the world.

#swift development service providers #best swift development companies #top swift development companies #swift development solutions #top swift developers #swift

Hire Dedicated Swift Developers

Want to create a native iOS application for your Startup?

Hire Dedicated Swift Developers for end-to-end services like development, migration, upgrade, testing, and support & maintenance. Trust HourlyDeveloper.io our Swift development team for iOS device apps that are high on performance and security.

Consult with experts:- https://bit.ly/2C5M6cz

#hire dedicated swift developers #swift developers #swift development company #swift development services #swift development #swift

Jamel  O'Reilly

Jamel O'Reilly

1658123700

Promis: The Easiest Future and Promises Framework in Swift

Promis

The easiest Future and Promises framework in Swift. No magic. No boilerplate.

Overview

While starting from the Objective-C implementation of JustPromises and keeping the code minimalistic, this library adds the following:

  • conversion to Swift 4
  • usage of generics to allow great type inference that wasn't possible in Objective-C
  • overall refactoring for fresh and modern code
  • remove the unnecessary and misleading concept of Progress causing bad patterns to emerge

You can read about the theory behind Future and Promises on Wikipedia, here are the main things you should know to get started.

  • Promises represent the promise that a task will be fulfilled in the future while the future holds the state of such resolution.
  • Futures, when created are in the unresolved state and can be resolved with one of 3 states: with a result, an error, or being cancelled.
  • Futures can be chained, allowing to avoid the pyramid of doom problem, clean up asynchronous code paths and simplify error handling.

Promis brags about being/having:

  • Fully unit-tested and documented 💯
  • Thread-safe 🚦
  • Clean interface 👼
  • Support for chaining ⛓
  • Support for cancellation 🙅‍♂️
  • Queue-based block execution if needed 🚆
  • Result type provided via generics 🚀
  • Keeping the magic to the minimum, leaving the code in a readable state without going off of a tangent with fancy and unnecessary design decisions ಠ_ಠ

Alternatives

Other open-source solutions exist such as:

Promis takes inspiration from the Objective-C version of JustPromises developed by the iOS Team of Just Eat which is really concise and minimalistic, while other libraries are more weighty.

Usage

The following example should outline the main benefits of using futures via chaining.

let request = URLRequest(url: URL(string: "http://example.com")!)

// starts by hitting an API to download data
getData(request: request).thenWithResult { data in
    // continue by parsing the retrieved data
    parse(data: data)
}.thenWithResult { parsedData in
    // continue by mapping the parsed data
    map(data: parsedData)
}.onError { error in
    // executed only in case an error occurred in the chain
    print("error: " + String(describing: error))
}.finally(queue: .main) { future in
    // always executed, no matter the state of the previous future or how the chain did perform
    switch future.state {
        case .result(let value):
            print(String(describing: value))
        case .error(let err):
            print(String(describing: err))
        case .cancelled:
            print("future is in a cancelled state")
        case .unresolved:
            print("this really cannot be if any chaining block is executed")
        }
}

The functions used in the example have the following signatures:

func getData(request: URLRequest) -> Future<Data>
func parse(data: Data) -> Future<[Dictionary<String,AnyObject>]>
func map(data: [Dictionary<String,AnyObject>]) -> Future<[FooBar]>

Promises and Futures are parametrized leveraging the power of the generics, meaning that Swift can infer the type of the result compile type. This was a considerable limitation in the Objective-C world and we can now prevent lots of issues at build time thanks to the static typing nature of the language. The state of the future is an enum defined as follows:

enum FutureState<ResultType> {
    case unresolved
    case result(ResultType)
    case error(Error)
    case cancelled
}

Promises are created and resolved like so:

let promise = Promise<ResultType>()
promise.setResult(value)
// or
promise.setError(error)
// or
promise.cancel()

Continuation methods used for chaining are the following:

func then<NextResultType>(queue: DispatchQueue? = nil, task: @escaping (Future) -> Future<NextResultType>) -> Future<NextResultType>
func thenWithResult<NextResultType>(queue: DispatchQueue? = nil, continuation: @escaping (ResultType) -> Future<NextResultType>) -> Future<NextResultType> {
func onError(queue: DispatchQueue? = nil, continuation: @escaping (Error) -> Void) -> Future {
func finally(queue: DispatchQueue? = nil, block: @escaping (Future<ResultType>) -> Void)

All the functions can accept an optional DispatchQueue used to perform the continuation blocks.

Best practices

Functions wrapping async tasks should follow the below pattern:

func wrappedAsyncTask() -> Future<ResultType> {

    let promise = Promise<Data>()
    someAsyncOperation() { data, error in
        // resolve the promise according to how the async operations did go
        switch (data, error) {
        case (let data?, _):
            promise.setResult(data)
        case (nil, let error?):
            promise.setError(error)
        // etc.
        }
    }
    return promise.future
}

You could chain an onError continuation before returning the future to allow in-line error handling, which I find to be a very handy pattern.

// ...
return promise.future.onError {error in
    // handle/log error
}

Pitfalls

When using then or thenWithResult, the following should be taken in consideration.

...}.thenWithResult { data -> Future<NextResultType> in
    /**
    If a block is not trivial, Swift cannot infer the type of the closure and gives the error
    'Unable to infer complex closure return type; add explicit type to disambiguate'
    so you'll have to add `-> Future<NextResultType> to the block signature
    
    You can make the closure complex just by adding any extra statement (like a print).
    
    All the more reason to structure your code as done in the first given example :)
    */
    print("complex closure")
    return parse(data: data)
}

Please check the GettingStarted playground in the demo app to see the complete implementation of the above examples.

Installation

CocoaPods

Add Promis to your Podfile

use_frameworks!
target 'MyTarget' do
    pod 'Promis', '~> x.y.z'
end
$ pod install

Carthage

github "albertodebortoli/Promis" ~> "x.y.z"

Then on your application target Build Phases settings tab, add a "New Run Script Phase". Create a Run Script with the following content:

/usr/local/bin/carthage copy-frameworks

and add the following paths under "Input Files":

$(SRCROOT)/Carthage/Build/iOS/Promis.framework

Author

Alberto De Bortoli albertodebortoli.website@gmail.com Twitter: @albertodebo GitHub: albertodebortoli website: albertodebortoli.com

License

Promis is available under the Apache 2 license in respect of JustPromises which this library takes inspiration from. See the LICENSE file for more info.

Author: albertodebortoli
Source Code: https://github.com/albertodebortoli/Promis
License: MIT license

#ios #swift 

Best Swift App Development Company

Hire an Exceptional Swift App Developer from Mobile App Development India. Maadi has a dedicated Swift App Development team that is superiorly talented and builds highly functional, cost-effective mobile apps with error-free coding.

Contact: https://www.mobile-app-development-india.com/swift-app-development/

#swift ios app development india #hire swift programmer india #swift ios development #apple swift app development #swift mobile app development #swift app development