RealityView Not Refreshing With SwiftData

Hi, I am trying to update what entities are visible in my RealityView. After the SwiftData set is updated, I have to restart the app for it to appear in the RealityView.

Also, the RealityView does not close when I move to a different tab. It keeps everything on and tracking, leaving the model in the same location I left it.

import SwiftUI
import RealityKit
import MountainLake
import SwiftData

struct RealityLakeView: View {
    
    @Environment(\.modelContext) private var context
    
    @Query private var items: [Item]
    
    
    var body: some View {
    
        
        
        RealityView { content in
            
            print("View Loaded")
            
            let lakeScene = try? await Entity(named: "Lake", in: mountainLakeBundle)
            let anchor = AnchorEntity(.plane(.horizontal, classification: .any, minimumBounds: SIMD2<Float>(0.2, 0.2)))
            
            @MainActor func addEntity(name: String) {
                if let lakeEntity = lakeScene?.findEntity(named: name) {
                                    // Add the Cube_1 entity to the RealityView
                                    anchor.addChild(lakeEntity)
                } else {
                    print(name + "entity not found in the Lake scene.")
                }
            }
            
           


                addEntity(name: "Island")
                for item in items {
                    if(item.enabled) {
                        addEntity(name: item.value)
                    }
                }
            

            // Add the horizontal plane anchor to the scene
            content.add(anchor)
            
            

            content.camera = .spatialTracking

        } placeholder: {
            ProgressView()
        }

        .edgesIgnoringSafeArea(.all)
    }
    
    
}

#Preview {
    RealityLakeView()
}

More info:

I am using Reality Composer pro to create the 3d content, each category with a different transform. The transform is then linked to an Item by name.

I then want the RealityView to show any Items in items that have enabled=true.

Not sure if their might be a better way to implement this.

Update.

I figured out if I can kill the RealityView when I move to lets say the home tab instead of the AR tab, it would have to refresh the entities anyway.

What's the best way to pause/kill the view when I move to a different view/tab?

(I also noticed that performance takes a hit in other areas if it stays open.)

Here's a similar thread issue https://forums.developer.apple.com/forums/thread/771721

RealityView Not Refreshing With SwiftData
 
 
Q