Daily iOS
SwiftUI

SwiftUI API loading state

A SwiftUI list screen loads data from a hotel API. To handle loading, success, no results, and error on screen without missing one, how would you model the state?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

Keep one state enum with loading, success, empty, and error cases as the screen state. Drawing the four screens with a switch in body lets the compiler catch a missing case, and empty results are distinguished from success. Attach the request with .task so it's canceled automatically when the screen goes away.

Share a question