I'm trying to save then load an ARKit `ARWorldMap` to a local file. I seem to have the saving working fine:
func saveWorldMap() {
ARView?.session.getCurrentWorldMap { [unowned self] worldMap, error in
guard let map = worldMap else { return }
do {
let data = try NSKeyedArchiver.archivedData(withRootObject: map, requiringSecureCoding: true)
do {
let url: URL = URL(fileURLWithPath: self.worldMapFilePath("test"))
try data.write(to: url)
} catch {
fatalError("Can't write to url")
}
} catch {
fatalError("Can't encode map")
}
}
}
func worldMapFilePath(_ fileName: String) -> String {
createWorldMapsFolder()
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0] as String
let filePath: String = "\(documentsDirectory)/WorldMaps/WorldMap_\(fileName)"
if FileManager().fileExists(atPath: filePath) { try! FileManager().removeItem(atPath: filePath) }
return filePath
}
func createWorldMapsFolder() {
let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
if let documentDirectoryPath = documentDirectoryPath {
let replayDirectoryPath = documentDirectoryPath.appending("/WorldMaps")
let fileManager = FileManager.default
if !fileManager.fileExists(atPath: replayDirectoryPath) {
do {
try fileManager.createDirectory(atPath: replayDirectoryPath, withIntermediateDirectories: false, attributes: nil)
} catch {
print("Error creating Captures folder in documents dir: \(error)")
}
} else {
print("WorldMaps folder already created. No need to create.")
}
}
}
The problem comes when I try to then load that saved ARWorldMap:
func loadWorldMap() {
guard let data = retrieveWorldMapDataForFilename("test") else { fatalError("Can't get data") }
do {
let worldMap = try NSKeyedUnarchiver.unarchivedObject(ofClass: ARWorldMap.self, from: data)
startSession(options: [.resetTracking, .removeExistingAnchors], initialWorldMap: worldMap)
} catch {
fatalError("Can't get worldMap")
}
}
func retrieveWorldMapDataForFilename(_ filename: String) -> Data? {
let url: URL = URL(fileURLWithPath: self.worldMapFilePath(filename))
do {
let data = try Data(contentsOf: url)
return data
} catch {
fatalError("Can't get data at url:\(url)")
}
}
When this I try to load the saved `ARWorldMap` with `loadWorldMap()` the bottom fatalError there in `retrieveWorldMapDataForFilename(_ filename: String)`, is caught with the following error
Thread 1: Fatal error: Can't get data at url:file:///var/mobile/Containers/Data/Application/924B012B-B149-4BA7-BFC2-BB79849D866F/Documents/WorldMaps/WorldMap_test
What am I doing wrong?