2

I'm encountering problems saving a document as I can see in the debug area:

NSFileVersion tried to tried to add a new generation and failed. Versioned file URL: file:///Users/mike97/Downloads/test%20copy.xml, contents URL: file:///Users/mike97/Downloads/test%20copy~.xml, error: Error Domain=GSLibraryErrorDomain Code=2 "Unable to open /Users/mike97/Downloads/test copy~.xml" UserInfo={NSDescription=Unable to open /Users/mike97/Downloads/test copy~.xml, NSUnderlyingError=0x608000856020 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory" UserInfo={NSDescription=No such file or directory}}}
2017-06-19 20:19:53.069020+0200 XML Parser[2095:54823] NSDocument failed to preserve the old version of a document. Here's the error:
Error Domain=GSLibraryErrorDomain Code=2 "Unable to open /Users/mike97/Downloads/test copy~.xml" UserInfo={NSDescription=Unable to open /Users/mike97/Downloads/test copy~.xml, NSUnderlyingError=0x608000856020 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory" UserInfo={NSDescription=No such file or directory}}}
2017-06-19 20:19:53.118611+0200 XML Parser[2095:54823] <XML_Parser.Document: 0x6000001014d0>: An error occurred while attempting to preserve the backup file at file:///Users/mike97/Downloads/test%20copy~.xml: Error Domain=GSLibraryErrorDomain Code=2 "Unable to open /Users/mike97/Downloads/test copy~.xml" UserInfo={NSDescription=Unable to open /Users/mike97/Downloads/test copy~.xml, NSUnderlyingError=0x608000856020 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory" UserInfo={NSDescription=No such file or directory}}} ##

The app try to atomically save a file called 'test copy.xml' and doing this just rename that file to 'test copy~.xml', why cannot found the original one. In fact changing the option parameter saving the data the error gone. This is the code I'm using in my NSDocument subclass:

    override func writeSafely(to url: URL, ofType typeName: String, for saveOperation: NSDocument.SaveOperationType) throws {
            if let vc = self.windowController?.contentViewController as? ViewController {
                switch typeName {
                case "XML v1":
                    do {
                        try vc.parser?.save().write(to: url, options: [])
                    } catch  {
                        throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
                    }
                    break
..

In NSData.WritingOptions we can found some of the options to save a file, but I'm not sure what of them I need. Anyone care to explain me what they are and what are the best using the 'writeSafely' method chosen by me?

Also that method handles possible errors, so that a non-atomic write operation appear to be enough, but I wonder on how can I preserve the 'versioning' functionality by macOS.

Community
  • 1
  • 1
Mike97
  • 596
  • 5
  • 20

0 Answers0