Turn asynchronous functions into synchronous, optimise your app flow and more.

When you write mobile apps in Swift, you usually have a lot of background work. I’ve been working as a mobile developer for almost 10 years and I can hardly remember a single project without Internet requests. Each Internet request requires time to be processed. Usually an unknown amount of time, possibly, endless.

If you do such work in the main or UI (which is the same) thread, your UI will get stuck. That’s why asynchronous tasks in Swift are designed the way they will never do so. The most common way is to avoid it is to use a callback or delegate. For example:

API.getUserInfo(userId: userId) { (userInfo, error) in
}

Here the app flow stops only for a small fraction of a second to prepare and send a request, but it doesn’t wait for the result. We get the result in a closure. userInfo contains information about a user (or nil if error happened), error is an optional error.

#ios-app-development #mobile-app-development #swift #asynchronous #synchronicity

Writing Synchronous Code in Swift
1.90 GEEK