Unexpected behavior when writing entities and loading realityFiles.

I have a simple visionOS app that creates an Entity, writes it to the device, and then attempts to load it. However, when the entity file get overwritten, it affects the ability for the app to load it correctly.

Here is my code for saving the entity.

import SwiftUI
import RealityKit
import UniformTypeIdentifiers
struct ContentView: View {

    var body: some View {
        VStack {
            ToggleImmersiveSpaceButton()
            Button("Save Entity") {
                Task {
                    //        if let entity = await buildEntityHierarchy(from: urdfPath) {
                    let type = UTType.realityFile
                    let filename = "testing.\(type.preferredFilenameExtension ?? "bin")"
                    let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
                    let fileURL = documentsURL.appendingPathComponent(filename)
                    
                    do {
                        let mesh = MeshResource.generateBox(size: 1, cornerRadius: 0.05)
                        let material = SimpleMaterial(color: .blue, isMetallic: true)
                        
                        
                        let modelComponent = ModelComponent(mesh: mesh, materials: [material])
                        
                        
                        let entity = Entity()
                        entity.components.set(modelComponent)
                        print("Writing \(fileURL)")
                        try await entity.write(to: fileURL)
                        
                    } catch {
                        print("Failed writing")
                    }
                }
            }
        }
        .padding()
    }
}

Every time I press "Save Entity", I see a warning similar to:

Writing file:///var/mobile/Containers/Data/Application/1140E7D6-D365-48A4-8BED-17BEA34E3F1E/Documents/testing.reality
Failed to set dependencies on asset 1941054755064863441 because NetworkAssetManager does not have an asset entity for that id.

When I open the immersive space, I attempt to load the same file:

import SwiftUI
import RealityKit
import UniformTypeIdentifiers

struct ImmersiveView: View {
    @Environment(AppModel.self) private var appModel

    var body: some View {
        RealityView { content in
            
            guard
                let type = UTType.realityFile.preferredFilenameExtension
            else {
                return
            }
            
            let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
            let fileURL = documentsURL.appendingPathComponent("testing.\(type)")
            
            guard FileManager.default.fileExists(atPath: fileURL.path) else {
                print("❌ File does not exist at path: \(fileURL.path)")
                return
            }
            
            if let entity = try? await Entity(contentsOf: fileURL) {
                content.add(entity)
            }
        }
    }
}

I also get errors after I overwrite the entity (by pressing "Save Entity" after I have successfully loaded it once). The warnings that appear when the Immersive space attempts to load the new entity are:

Asset 13277375032756336327 Mesh (RealityFileAsset)URL/file:///var/mobile/Containers/Data/Application/1140E7D6-D365-48A4-8BED-17BEA34E3F1E/Documents/testing.reality/Mesh_0.compiledmesh failure: Asset provider load failed: type 'RealityFileAsset' -- RERealityArchive: Failed to open load stream for entry 'assets/Mesh_0.compiledmesh'.
Asset 8308977590385781534 Scene (RealityFileAsset)URL/file:///var/mobile/Containers/Data/Application/1140E7D6-D365-48A4-8BED-17BEA34E3F1E/Documents/testing.reality/Scene_0.compiledscene failure: Asset provider load failed: type 'RealityFileAsset' -- RERealityArchive: Failed to read archive entry.
AssetLoadRequest failed because asset failed to load '13277375032756336327 Mesh (RealityFileAsset)URL/file:///var/mobile/Containers/Data/Application/1140E7D6-D365-48A4-8BED-17BEA34E3F1E/Documents/testing.reality/Mesh_0.compiledmesh' (Asset provider load failed: type 'RealityFileAsset' -- RERealityArchive: Failed to open load stream for entry 'assets/Mesh_0.compiledmesh'.)

The order of operations to make this happen:

  1. Launch app
  2. Press "Save Entity" to save the entity
  3. "Open Immersive Space" to view entity
  4. Press "Save Entity" to overwrite the entity
  5. "Open Immersive Space" to view entity, failed asset load request

Also

  1. Launch app, the entity should still be save from last time the app ran
  2. "Open Immersive Space" to view entity
  3. Press "Save Entity" to overwrite the entity
  4. "Open Immersive Space" to view entity, failed asset load request

NOTE: It appears I can get it to work slightly better by pressing the "Save Entity" button twice before attempting to view it again in the immersive space.

Unexpected behavior when writing entities and loading realityFiles.
 
 
Q