save json watchOS

Hi, I'm trying to save a json into my Apple Watch using:

enum Directory {
        // Only documents and other data that is user-generated, or that cannot otherwise be recreated by your application, should be stored in the <Application_Home>/Documents directory and will be automatically backed up by iCloud.
        case documents
        
        // Data that can be downloaded again or regenerated should be stored in the <Application_Home>/Library/Caches directory. Examples of files you should put in the Caches directory include database cache files and downloadable content, such as that used by magazine, newspaper, and map applications.
        case caches
    }
    
    /// Returns URL constructed from specified directory
    static fileprivate func getURL(for directory: Directory) -> URL {
        var searchPathDirectory: FileManager.SearchPathDirectory
        
        switch directory {
        case .documents:
            searchPathDirectory = .documentDirectory
        case .caches:
            searchPathDirectory = .cachesDirectory
        }
        
        if let url = FileManager.default.urls(for: searchPathDirectory, in: .userDomainMask).last {
            return url
        } else {
            fatalError("Could not create URL for specified directory!")
        }
    }
    
    
    /// Store an encodable struct to the specified directory on disk
    ///
    /// - Parameters:
    ///   - object: the encodable struct to store
    ///   - directory: where to store the struct
    ///   - fileName: what to name the file where the struct data will be stored
    static func store<T: Encodable>(_ object: T, to directory: Directory, as fileName: String) {
        let url = getURL(for: directory).appendingPathComponent(fileName, isDirectory: false)
        
        let encoder = JSONEncoder()
        encoder.outputFormatting = .prettyPrinted
        do {
            let data = try encoder.encode(object)
            if FileManager.default.fileExists(atPath: url.path) {
                try FileManager.default.removeItem(at: url)
            }
            FileManager.default.createFile(atPath: url.path, contents: data, attributes: nil)
            //Mostrar el JSON guardado
            //let jsonString = String(data: data, encoding: .utf8)!
            // print("CADENA JSON: \(jsonString)")
        } catch {
            fatalError(error.localizedDescription)
        }
    }

If I force the exit of the App, when I relaunch it I can't find my json. And If I wait a pair of days my json is deleted. I don't know if this is normal in development mode or I have to change something in Xcode.

Thanks

Replies

I have not tried FileManager on the watch. NSUserDefaults works well.

Thanks for your reply, because trying NSUserDefaults I discovered that the problem was when I was trying to search the json saved:

if Bundle.main.path(forResource: "myFile", ofType: "json") != nil

This line doesn't find the file and then... the json isn't recovered after a Apple Watch reboot.

Thanks