tgebarowski / thepromisedland

Demo project demonstrating how to build a service layer with promises, request behaviours and child view controllers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The promised land

Demo project demonstrating how to build a service layer with promises, request behaviours and child view controllers.

Motivation

Writing asynchronous code in Swift can be a cumbersome process when chaining requests, adding proper error handling and indicating status of network operation.

When chaining two or more request or functions invoked asynchronosuly, what we do frequently is nesting completion handlers and adding invocation of same error handler in several places, same with handling network activity indicator:

    func trigger() {
    	showActivityIndicator()
        send(completion: { [weak self] (model) in
            self?.ack(model: model, completion: {
                self?.hideActivityIndicator()
            }, error: { (error) in
                self?.hideActivityIndicator()
                self?.handle(error: error)
            })
        }) { (error) in
            self?.hideActivityIndicator()
            self?.handle(error: error)
        }
    }

What if this code, could be simply transformed to:

   func trigger() {
        send().then { [weak self] (model) in
            return self?.ack(model: model)
        }.then { (_) in
           ...
        }
    }

How to compile?

carthage update --platform iOS

and then compile with Xcode 10+

Presentation

This project was created for the purpose of "The promised land" presentation, which content can be found here.

References

This Demo project demonstrates a design pattern based on Promises, Request Behaviours and Child View Controllers.

Inspired and based on great work from Soroush Khanlou

About

Demo project demonstrating how to build a service layer with promises, request behaviours and child view controllers

License:Apache License 2.0


Languages

Language:Swift 100.0%