title: Saving and Loading ARWorldMap in RealityKit
body: <p>I am using SwiftUI and RealityKit to save my world map, along with one entity (box) that I will place in the world.</p>
  <p>Inside my Coordinator I have the following code to create a box and add it to the anchor entity.</p>
  <pre><code> @objc func onTap(_ recognizer: UITapGestureRecognizer) {
          
          guard let arView = arView else {
              return
          }
          
          let location = recognizer.location(in: arView)
          
          let results = arView.raycast(from: location, allowing: .estimatedPlane, alignment: .horizontal)
          
          if let result = results.first {
              
              let arAnchor = ARAnchor(name: &quot;boxAnchor&quot;, transform: result.worldTransform)
             
              let anchorEntity = AnchorEntity(anchor: arAnchor)
              let box = ModelEntity(mesh: MeshResource.generateBox(size: 0.3), materials: [SimpleMaterial(color: .green, isMetallic: true)])
              
              arView.session.add(anchor: arAnchor)
              anchorEntity.addChild(box)
              arView.scene.addAnchor(anchorEntity)
          }
      }
  </code></pre>
  <p><strong>When I click Save button it saves the worldMap</strong></p>
  <pre><code> func saveWorldMap() {
          
          guard let arView = arView else {
              return
          }
          
          arView.session.getCurrentWorldMap { worldMap, error in
              
              if let error = error {
                  print(error)
                  return
              }
              
              if let worldMap = worldMap {
                  
                  guard let data = try? NSKeyedArchiver.archivedData(withRootObject: worldMap, requiringSecureCoding: true) else {
                      return
                  }
                  
                  // save the data into user defaults
                  let userDefaults = UserDefaults.standard
                  userDefaults.set(data, forKey: &quot;worldMap&quot;)
                  userDefaults.synchronize()
                  
              }
          }
          
      }
  </code></pre>
  <p>And finally, the loadWorldMap function is supposed to load the map with the anchors and the entities attached to the anchors. Unfortunately, it does load the ARAnchors but there are no entities attached to them. The main reason being that I attached the entity to the AnchorEntity and not ARAnchor. How can I save an entity like a box attached to the ARAnchor.</p>
  <pre><code> func loadWorldMap() {
          
          guard let arView = arView else {
              return
          }
          
          let userDefaults = UserDefaults.standard
          
          if let data = userDefaults.data(forKey: &quot;worldMap&quot;) {
              
              print(data)
             
              print(&quot;loading world map&quot;)
              guard let worldMap = try? NSKeyedUnarchiver.unarchivedObject(ofClass: ARWorldMap.self, from: data) else {
                  return
              }
              
              for anchor in worldMap.anchors {
                  print(anchor.name)
              }
              
              let configuration = ARWorldTrackingConfiguration()
              configuration.initialWorldMap = worldMap
              configuration.planeDetection = .horizontal
              
              arView.session.run(configuration)
              
          }
      }
  </code></pre>