Skip to main content

Posts

Showing posts from March, 2020

Swift, architecture, asynchronousness

Asynchronous calls are one of the elements that have a strong influence on the structure of the whole application. However, they are often treated as a necessary evil or even avoided, if possible. At the same time, iOS itself and the swift language syntax provide tools to make asynchronous calls easier to use.

Swift, like JavaScript asynchronous calls are based on callbacks. This means that when calling an asynchronous function, one of the arguments is another function (or code block) to be called when the asynchronous work is finished. Although the action is different from a synchronous call, the code is nevertheless similar. For example:


func syncFunction() -> String { //some work here return "result" } //call sync let x = syncFunction() print(x) func asyncFunction(callback: @escaping (String) -> Void) { DispatchQueue.global().async { //some work callback("result") } } //async call asyncFunction() { res in …