核心数据:删除未保存的对象时出错
我在核心数据项目上遇到了一些麻烦。我有一个 NSArrayController,其中包含 NSManagedObjects 和 + / - 按钮。如果我在保存文件后删除行中的一个对象,它可以正常工作,但是如果我添加一个新对象,然后立即再次删除它(不更改任何默认值),我会收到错误:
Serious application error. Exception was caught during Core Data change processing: Unknown number type or nil passed to arithmetic function expression. with userInfo (null)
Unknown number type or nil passed to arithmetic function expression.
情况相同如果我撤消新对象的添加。
有人可以指点我该怎么做吗?我认为这与它只有一个临时 ID 有关,但我不知道如何解决。
I have some trouble with a core data project. I have a NSArrayController with NSManagedObjects in it, and + / - buttons. If i delete a object in the row after the file has been saved it works perfectly, but if I add a new object, and immediately delete it again (without changing any of the default values), i get an error:
Serious application error. Exception was caught during Core Data change processing: Unknown number type or nil passed to arithmetic function expression. with userInfo (null)
Unknown number type or nil passed to arithmetic function expression.
It's the same case if i undo the add of the new object.
Anybody able to give me a pointer what to do? I assume it has something to do with that it only has an temporary ID, but how to solve it i don't know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了问题和解决方案。为了记录在案,我将描述它。
问题在于该对象在其 init 函数中添加了一个观察者。这可能是引发错误的原因。如果我在删除对象之前将其自身作为观察者删除,则不会产生错误。
I found the problem and solution. For the record i will describe it.
The problem was that the object in its init function added an observer on itself. This was probably what triggered the error. If i before i delete the object remove itself as observer, the error is not produced.
表通常不包含 NSManagedObjects。相反,要么 NSArrayController 通过绑定提供托管对象的属性作为值,要么是符合 NSTableViewDataSource 的对象。可以通过多种方式来删除在这些场景中的任一个中提供的选定对象。问题是,你根本没有解释你的案件是如何建立的。
您的删除(无论您的按钮连接到什么目标/操作)是否可能被传递为零或出现意外情况?如果您要求托管对象上下文删除浮点值或其他一些随机的东西,我可以想象一个类似的可怕错误......
Tables don't usually contain NSManagedObjects. Rather, either an NSArrayController is providing the managed objects' attributes as values via bindings or an object conforming NSTableViewDataSource is. The deletion of a selected object provided in either of these scenarios can be done a myriad of ways. The problem is, you haven't explained at all how your case is set up.
Is it possible your deletion (whatever target/action your button is wired to) is being passed nil or something unexpected? I can imagine a similarly-frighenting error if you asked a managed object context to delete a float value or some other random thing ...