Search code examples
iosswiftarkitnskeyedarchivernskeyedunarchiver

Swift iOS ARKit Load ARWorldMap Data from File NSKeyedUnarchiver NSKeyedArchiver


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?


Solution

  • Looking at your code, the issue lies within the following function:

    func worldMapFilePath(_ fileName: String) -> String {}
    

    This function works fine when you save the World Map, but when you try to retrieve it, the file is getting deleted.

    If you log the actual error:

    fatalError("Error = \(error)")
    

    rather than using this:

    fatalError("Can't get data at url:\(url)")
    

    You will get the following error message:

    "The file “WorldMap_test” couldn’t be opened because there is no such file.

    Which is more informative than the one provided in your question e.g:

    Can't get data at url:file:///var/mobile/Containers/Data/Application/924B012B-B149-4BA7-BFC2-BB79849D866F/Documents/WorldMaps/WorldMap_test

    As such some simple changes will solve your issue.

    Firstly I recommend moving your call to the createWorldMapsFolder() to viewDidLoad.

    Secondly change your func worldMapFilePath(_ fileName: String) -> String {} like so:

      func worldMapFilePath(_ fileName: String) -> String {
    
            let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
            let documentsDirectory = paths[0] as String
            let filePath: String = "\(documentsDirectory)/WorldMaps/WorldMap_\(fileName)"
            return filePath
    
        }
    

    Then move your fileExists call to your saveWorldMap function e.g:

    func saveWorldMap() {
    
            sceneView.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 worldMapPath = self.worldMapFilePath("test")
    
                        if FileManager().fileExists(atPath: worldMapPath) { try! FileManager().removeItem(atPath: worldMapPath) }
    
                        let url: URL = URL(fileURLWithPath: worldMapPath)
                        try data.write(to: url)
    
                    } catch {
                        fatalError("Can't write to url")
                    }
                } catch {
                    fatalError("Can't encode map")
                }
            }
    
        }
    

    Hope it helps...