Clean AppDelegate

How can you make sure your AppDelegate has a single responsibility?

Steven Curtis
The Startup

--

The App Delegate has many responsibilities by default, including crash reporting, analytics, setting up the CoreData stack, notifications etc.

This breaks the Single Responsibility principle because the AppDelegate handles too many things.

The AppDelegate is responsible for this mix of things that are caught up in the massive AppDelegate class. Even splitting up the responsibilities into
smaller methods just spreads the responsibilities around.

To avoid this we can implement a form of the strategy pattern, where the behaviours of a class are encapsulated by using interfaces.

Prerequisites:

  • Some knowledge of creating iOS Apps would be useful

Terminology

AppDelegate: Effectively the root object of an iOS App, working in conjunction with UIApplication to manage interactions with the system.

The code

Within the App delegate we can collect services in an array:

var services: [UIApplicationDelegate] = [

PersistenceService(),

AnalyticsService(),

--

--