Image for post
Image for post

Network layer is very common and essential part of modern mobile apps. It usually takes the form of a manager class that takes care of every network request throughout your app.

Considering the separation of concern, a good networking layer is usually decoupled from the view code. Logic controllers or view controllers use the network manager to request certain API calls for example, and subsequently get a callback once the request succeeds or fails to reflect the result to the views. So ideally, the network manager should never know anything about the views.

The Problem

There’s a very unique situation that makes this separation of concerns difficult. When you get an error from the network, like 404 or 500, and you need to show some kind of alerts to the user with a proper error message. This error should show the same UI regardless of where this network manager is called. The code often lives in one place, and usually it’s the root view controller of the whole app. How would you get a reference to the root view controller from the network manager, when the network manager is being called from a view controller deep down in the view hierarchy? …

Sean Choe

Senior iOS engineer @Patreon. Draw iOS comic on Instagram(deriveddata) and Twitter(@derived_data). Former UI designer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store