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

Hi @The-Technical-Scientist,

I'm not sure I follow exactly what's happening here, but reading the code it looks like the expectation is that the items query will contain all the items from the SwiftData storage that should be displayed. If items are added or removed from this list the expectation is that these new items will be run through the for item in items loop.

I also notice the query and other aspects of the 'world building' is happening in the create: block. If you add an update: block to your RealityView do you see more expected behavior?

Something like this:

    RealityView { content in
// keep this code more or less the same
    } update: { content in 
        for item in items {
            if(item.enabled) {
                 addEntity(name: item.value)
            }
        }
    }

Let me know how this change goes and redirect if I misunderstood the intent.

RealityView Not Refreshing With SwiftData
 
 
Q