NSManagedObject 子类的问题

发布于 2024-10-08 11:30:43 字数 319 浏览 2 评论 0原文

嘿伙计们,我有一个 NSManagedObject 的子类。在 awakeFromInsert 和 awakeFromFetch 中,我调用一个初始化方法,该方法除其他外还启动 NSTimer。

现在我需要一个地方来使计时器无效。但是,dealloc、finalize、didTurnIntoFault、prepareForDeletion 和 willTurnIntoFault 不会被调用。

根据文档,当对象从内存中清除时,这些方法都应该被调用。它们都不是,但是所有数据都保存在持久存储中。我很困惑为什么或如何。

我可以做些什么可能导致这些方法在对象生命周期期间不被调用?

Hey guys, I've got a subclass of an NSManagedObject. In awakeFromInsert and awakeFromFetch I'm calling an initialization method which, among other things, starts an NSTimer.

Now i need a place to invalidate the timer. However, dealloc, finalize, didTurnIntoFault, prepareForDeletion and willTurnIntoFault aren't getting called.

According to the documentation, these methods should all get called when the object is cleared from memory. None of them are, however all the data is saved in the persistent store. I'm puzzled as to why or how.

Is there anything i could be doing that could cause these methods to not get called during the objects life cycle?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

疯狂的代价 2024-10-15 11:30:43

核心数据控制 NSManagedObject 的生命周期。除非您要求,否则它不会自行从内存中刷新对象。查看 ,似乎有两种方法:

  1. 发送 refresh:mergeChanges: 到 MOC 导致对象变成故障。
  2. 向 MOC 发送 reset 会使其自行重置,就像刚刚创建一样。

但是,上述任何操作都需要您执行显式操作,因此您不妨向对象添加一个方法以使其计时器无效并调用该方法。

事实上,您的问题可能表明设计问题。 NSTimer 本质上是一个用户界面事件。它可能应该由 MVC 控制器控制,该控制器向模型对象(NSManagedObject)发送消息来执行操作。

Core data controls the lifetime of NSManagedObjects. It's not going to flush an object from memory by itself unless you ask it to. Looking at the documentation, there appear to be two ways:

  1. sending refresh:mergeChanges: to the MOC causes the object to turn into a fault.
  2. sending reset to the MOC causes it to reset itself as if it has just been created.

However, any of the above requires explicit action on your part, so you might as well add a method to the object to invalidate its timer and invoke that.

In fact, your problem probably indicates a design issue. An NSTimer is essentially a user interface event. It should probably be controlled by your MVC controller which sends a message to the model object (the NSManagedObject) to do the action.

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