具有自动保存功能的 CoreData 第一次保存,然后因“NoSuchFileError”而崩溃在所有以下自动保存中

发布于 2024-11-05 11:44:33 字数 869 浏览 4 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠北森屋 2024-11-12 11:44:33

为什么你试图每秒自动保存十次?这种情况太频繁了。实际保存文件可能需要超过十分之一秒的时间。更合理的值应该是 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.

灯下孤影 2024-11-12 11:44:33

从字里行间看,这似乎是 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):

“Save To…” and Autosave are not
directly supported—Core Data cannot
save to a store and maintain the same
changed state in the managed object
context, all the while maintaining an
unsaved stack as the current document.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文