[SwiftUI] Manipulating SwiftUI View Body during runtime

Hello,

I have a standalone WatchOS project using SwiftUI for the interface. During the App's init (), I trigger a Swift function call, that does some background activity in a worker thread. By default, the SwiftUI View shows a single label with "Hello World". The result of the worker thread will require an arbitary View to be shown, for eg., I want to show a View with four Labels now. In my use case, the layout for the View, can only be calculated at runtime.

Is there a way to arbitrarily manipulate a SwiftUI based View from Swift code at runtime?

Thanks!

It would help if you showed your code and where the error appears.

There is no error per se. My doubt is about how to dynamically use swiftui on the fly. To give an idea on the flow:

// MyApp.swift

import SwiftUI

@main
struct MyApp: App {
     
     let obj = MyWorkerClass ()

 init ()
     {
         obj.callEntryPoint ()
     }

 var body: some Scene {

         WindowGroup {
             ContentView ()
         }
    }
}
// ContentView.swift

import SwiftUI

struct ContentView: View {

 var body: some View {

        VStack {

            Image(systemName: "globe")

                .imageScale(.large)

                .foregroundColor(.accentColor)

            Text("Hello, world!")

        }

        .padding()

    }

}
// MyWorkerClass.swift

class MyWorkerClass
{

 func callEntyPoint ()
 {
     // Switch to a worker thread and do some operations

      DispatchQueue.global(qos: .background).async {
      
      // Once the operations are done, Show some UI change on the Main thread

      DispatchQueue.main.async {

            // Create a new Label on the screen 
       }

      }

 }

}
[SwiftUI] Manipulating SwiftUI View Body during runtime
 
 
Q