具有自动保存功能的 CoreData 第一次保存,然后因“NoSuchFileError”而崩溃在所有以下自动保存中
使用 CoreData 的基于 NSDocument 的应用程序。非常简单 - 两个实体,具有一对多关系。这个应用程序与保存、加载等功能完美配合。
然后我像这样打开自动保存:
[[NSDocumentController sharedDocumentController] setAutosavingDelay:0.1];
...然后它立即出现了严重错误。
第一次自动保存有效,100%正确。即,它将文件保存在用户的私人目录中,如果您退出应用程序并重新启动,自动保存的文档会自动重新打开。凉爽的!
但是,一旦您对数据进行第二次更改,自动保存就会崩溃,并显示:
error = Error Domain=NSCocoaErrorDomain Code=134030 UserInfo=0x1001a1be0 "An error occurred while saving."
Underlying Error=(Error Domain=NSCocoaErrorDomain Code=4 UserInfo=0x10019a840 "The file doesn’t exist."
Underlying Error=(Error Domain=NSPOSIXErrorDomain Code=2 UserInfo=0x100150d00 "The operation couldn’t be completed. No such file or directory"))
...这对我来说毫无意义。为什么 auto*SAVE* 会关心文件是否存在?为什么第一次会成功,但后来却失败了?
注意:我已经检查过了,Apple 尝试在第二次自动保存调用时使用相同的文件名 - 如果第一次成功,为什么 0.5 秒后失败了???
NSDocument-based application using CoreData. Very simple - two entities, with a one-to-many relationship. This app works perfectly with save, load, etc.
Then I turned on autosave like this:
[[NSDocumentController sharedDocumentController] setAutosavingDelay:0.1];
...and immediately it went horribly wrong.
The first autosave works, 100% correctly. i.e. it saves a file in the user's private directory, and if you quit the app and restart, the autosaved-doc automatically re-opens. Cool!
But as soon as you make a second change to the data, you get a crash in the autosave, with this:
error = Error Domain=NSCocoaErrorDomain Code=134030 UserInfo=0x1001a1be0 "An error occurred while saving."
Underlying Error=(Error Domain=NSCocoaErrorDomain Code=4 UserInfo=0x10019a840 "The file doesn’t exist."
Underlying Error=(Error Domain=NSPOSIXErrorDomain Code=2 UserInfo=0x100150d00 "The operation couldn’t be completed. No such file or directory"))
...which makes no sense to me. Why would auto*SAVE* care about whether a file exists or not? Why would it work the first time, then fail on subsequent times?
NB: I've checked, and Apple attemtps to use the identical filename on the second autosave call - if it worked first time, why did it fail 0.5 seconds later???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么你试图每秒自动保存十次?这种情况太频繁了。实际保存文件可能需要超过十分之一秒的时间。更合理的值应该是 10 秒,这可能比实际需要的频率更高。
Why are you attempting to auto-save ten times per second? That's WAY too often. It may take longer than a tenth of a second to actually save your file. A saner value would be 10 seconds and that's probably more frequent than is actually necessary.
从字里行间看,这似乎是 Apple 使用 NSDocument 实现 Core Data 的一个已知缺陷:自动保存已损坏,而且他们不打算修复它。
来自 NSPersistentDocument(当您在项目创建时选择 Core Data 和 NSDocument 时使用的 NSDocument 子类):
有趣的是,“核心数据不能……”——当然,CD“可以”做到这一点,尽管我可以看到它可能需要大量代码并跳过障碍才能使其工作。听起来更像是借口而不是解释。
Reading between the lines, this appears to be a known flaw of Apple's implementation of Core Data with NSDocument: autosave is broken, and they don't intend to fix it.
From NSPersistentDocument (the subclass of NSDocument that is used when you select Core Data and NSDocument at project creation time):
Interesting claim that "Core Data cannot..." - surely, CD "can" do this, although I can see it's probably a lot of code and jumping through hoops to make it work. Sounds more like an excuse than an explanation.