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.

I have the exact same issue. It would be great to get some insight from Apple on this. Errors that I've been getting are:

Error Domain=REAssetLoadRequestErrorDomain Code=200 "failed to load '5235212752866472828 CollisionShape (RealityFileAsset)URL/file:///var/mobile/Containers/Data/Application/1EED2404-D7C4-443E-9C28-0F8C23409BE4/Documents/RoutePaths/RoutePath.reality/CollisionShape_1.compiledcollisionshape' (Asset provider load failed: type 'RealityFileAsset' -- RERealityArchive: Failed to open load stream for entry 'assets/CollisionShape_1.compiledcollisionshape'.)" UserInfo={NSLocalizedDescription=failed to load '5235212752866472828 CollisionShape (RealityFileAsset)URL/file:///var/mobile/Containers/Data/Application/1EED2404-D7C4-443E-9C28-0F8C23409BE4/Documents/RoutePaths/RoutePath.reality/CollisionShape_1.compiledcollisionshape' (Asset provider load failed: type 'RealityFileAsset' -- RERealityArchive: Failed to open load stream for entry 'assets/CollisionShape_1.compiledcollisionshape'.)}
Asset 5235212752866472828 CollisionShape (RealityFileAsset)URL/file:///var/mobile/Containers/Data/Application/1EED2404-D7C4-443E-9C28-0F8C23409BE4/Documents/RoutePaths/RoutePath.reality/CollisionShape_1.compiledcollisionshape failure: Asset provider load failed: type 'RealityFileAsset' -- RERealityArchive: Failed to open load stream for entry 'assets/CollisionShape_1.compiledcollisionshape'.
AssetLoadRequest failed because asset failed to load '5235212752866472828 CollisionShape (RealityFileAsset)URL/file:///var/mobile/Containers/Data/Application/1EED2404-D7C4-443E-9C28-0F8C23409BE4/Documents/RoutePaths/RoutePath.reality/CollisionShape_1.compiledcollisionshape' (Asset provider load failed: type 'RealityFileAsset' -- RERealityArchive: Failed to open load stream for entry 'assets/CollisionShape_1.compiledcollisionshape'.)
Failed to set dependencies on asset 13239687683001190247 because NetworkAssetManager does not have an asset entity for that id.

Hi @ericD_TRI and @SkimoBen

Thanks for providing such a detailed question with reproducible code, that's very helpful. I was able to reproduce the issue you're experiencing. The problem appears to occur when deleting or overwriting a .reality file that's associated with an entity currently in the scene, which causes the "Failed to load" errors on subsequent loads.

Workaround:

Instead of reusing the same filename, create a unique file for each save operation. You can store the current filename in your appModel so the immersive view knows which file to load. For example:

let filename = "\(UUID().uuidString).\(type.preferredFilenameExtension ?? "bin")"
//...
try await entity.write(to: fileURL)
appModel.activeFileURL = fileURL

Finally, our engineering teams need to investigate this issue, as resolution may involve changes to Apple's software. I'd greatly appreciate it if you could open a bug report, reference this forum post, and post the FB number here once you do.

Bug Reporting: How and Why? has tips on creating your bug report.

Unexpected behavior when writing entities and loading realityFiles.
 
 
Q