Using Swift optionals to simplify asynchronous work

There is a subtle change in the latest template update. The variables viewController, interactor, and presenter are now declared as real optionals using ?. (They were forced unwrapped optionals with ! before.)


There was a situation where it could cause a crash if presenter is deallocated and a method is invoked on it.

This happens when the interactor does some asynchronous work. Before it can finish, the user has navigated away (such as tapping the back button) from the current scene.

Because the view is no longer needed, UIKit does the right thing to purge the view controller from memory. This frees up the memory for other tasks.

As a result, the whole VIP cycle is taken down as well. The view controller, interactor, and presenter are removed from memory.

This is all good and is the intended behavior. But it also means that when the result does come back from the asynchronous call, there’s no presenter to invoke.

self.presenter.presentFetchedOrders(response: response) causes a crash.

This was a nasty little bug that scratched some heads.

An obvious fix was to just check to see if presenter is nil before invoking it:

But that’s ugly.

With the new change using real optionals, we can take advantage of Swift’s built-in nil check:

Best of all, Xcode autocompletes the ? for you.

Simple. Clean. Effective.

Get the Clean Swift Xcode Templates

Join over 15,000 developers and subscribe to get my Xcode templates. Learn how to apply the VIP cycle to your projects. Extract business and presentation logic into interactor and presenter. Route to other scenes and pass data. Write fast, maintainable unit tests. Be confident making changes.

I promise I'll never send you spam. You can unsubscribe at any time.

I've been developing in iOS since the iPhone debuted, jumped on Swift when it was announced. Writing well-tested apps with a clean architecture has been my goal.

Leave a Comment

Your email address will not be published.