Concurrency (async + await)

I am having a problem updating the view with data after loading that data asynchronously.

Here is my code:

import SwiftUI
import Foundation

class CurrentUser {
    var id = ""
    var nickname = ""
}



class UserService {
    var currentUser = CurrentUser()
    static var shared = UserService()
    private init() {
    }
}



class ViewModel: ObservableObject {
    
    func fetchCurrentUser() async {
        
        let currentUser = UserService.shared.currentUser
        
        currentUser.id = "007"
        currentUser.nickname = "James Bond"
        
        // clearly I am missing something that would probably go here?
    }
}



struct ContentView: View {
    
    @StateObject private var viewModel = ViewModel()
    let currentUser = UserService.shared.currentUser
    
    var body: some View {
        VStack(spacing: 10) {
            Text("ID: \(currentUser.id)")
            Text("Nickname: \(currentUser.nickname)")
        }
        .task {
            await viewModel.fetchCurrentUser()
        }
    }
}

If I put a breakpoint in the ViewModel, I can type “po currentUser.id” into the console and see that the data has in fact loaded, but it is not being displayed in the view.

Any help would be greatly appreciated.

Turns out I found the answer in the iOS Foundations course Module 2, Lesson 8.

The problem was not with concurrency so much as properties, specifically creating some @Published properties in the ViewModel and then copying them into currentUser.