--------------------- | User List | --------------------- | * Alice | | * Bob | | * Charlie | | | | [Refresh] | ---------------------
Repository pattern in iOS Swift - Mini App: Build & Ship
import SwiftUI // TODO: Define User model here // TODO: Define UserRepository protocol here // TODO: Implement MockUserRepository here struct UserListView: View { // TODO: Add @State properties for users and loading state // TODO: Add repository property var body: some View { NavigationView { VStack { // TODO: Show loading indicator or user list Button("Refresh") { // TODO: Load users from repository } .padding() } .navigationTitle("User List") } .onAppear { // TODO: Load users when view appears } } } struct UserListView_Previews: PreviewProvider { static var previews: some View { UserListView() } }
import SwiftUI struct User: Identifiable { let id: Int let name: String } protocol UserRepository { func fetchUsers() async -> [User] } class MockUserRepository: UserRepository { func fetchUsers() async -> [User] { try? await Task.sleep(nanoseconds: 1_000_000_000) // 1 second delay return [ User(id: 1, name: "Alice"), User(id: 2, name: "Bob"), User(id: 3, name: "Charlie") ] } } struct UserListView: View { @State private var users: [User] = [] @State private var isLoading = false private let repository: UserRepository = MockUserRepository() var body: some View { NavigationView { VStack { if isLoading { ProgressView("Loading...") .padding() } else { List(users) { user in Text(user.name) } } Button("Refresh") { Task { await loadUsers() } } .padding() } .navigationTitle("User List") } .task { await loadUsers() } } private func loadUsers() async { isLoading = true users = await repository.fetchUsers() isLoading = false } } struct UserListView_Previews: PreviewProvider { static var previews: some View { UserListView() } }
We start by defining a simple User struct with an id and name. This lets SwiftUI identify each user uniquely in the list.
The UserRepository protocol declares a method fetchUsers() that returns users asynchronously. This hides the data source details from the UI.
MockUserRepository implements this protocol and simulates a network delay using Task.sleep. It returns a fixed list of users.
In UserListView, we use @State properties to hold the users and loading state. The repository is a constant instance of MockUserRepository.
The UI shows a ProgressView while loading, and a List of user names when done. The Refresh button triggers reloading.
We use Swift concurrency with async and await to fetch users without blocking the UI. The .task modifier loads users when the view appears.
This pattern cleanly separates data fetching logic from UI code, making it easier to maintain and test.
--------------------- | User List | --------------------- | * Alice | | * Bob | | * Charlie | | | | [Refresh] | ---------------------