← All topics/Networking & data

Question 1

Designing a networking layer

How would you design a scalable and maintainable networking layer for an iOS app?

Answer outline

Build the networking layer around clear separation of responsibilities. Three layers cover most apps:

  1. 1.Transport: makes requests and returns responses, nothing more. This is the only code that talks to URLSession.
  2. 2.Endpoint definitions: describe paths, methods, headers, query parameters, and bodies in one consistent shape.
  3. 3.Decoding and error mapping: convert raw responses into typed models and app-level failures, so feature code never sees a raw response.

Keep it protocol-driven, with a protocol at each boundary so features depend on abstractions. That lets you swap in a stub transport in tests.

Handle cross-cutting concerns such as authentication, retries, and logging in one shared layer so they don't drift between features. As the app grows, a team should be able to add an endpoint without rethinking the stack.

Principles

  • Give each layer one job, and treat a feature that calls URLSession directly as a sign the boundaries have leaked.
  • Put a protocol at each boundary so every layer can be tested against a stub.
  • Map network errors into typed app-level categories at the boundary, so feature code never inspects raw status codes.
  • Keep cross-cutting concerns such as authentication in one shared layer, so a change to retries or logging lands once.
  • A new endpoint should need only an endpoint definition and a response model, never a change to transport or decoding.