NSManagedObject 子类的问题
嘿伙计们,我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
核心数据控制 NSManagedObject 的生命周期。除非您要求,否则它不会自行从内存中刷新对象。查看 ,似乎有两种方法:
refresh:mergeChanges:
到 MOC 导致对象变成故障。reset
会使其自行重置,就像刚刚创建一样。但是,上述任何操作都需要您执行显式操作,因此您不妨向对象添加一个方法以使其计时器无效并调用该方法。
事实上,您的问题可能表明设计问题。 NSTimer 本质上是一个用户界面事件。它可能应该由 MVC 控制器控制,该控制器向模型对象(NSManagedObject)发送消息来执行操作。
Core data controls the lifetime of
NSManagedObject
s. 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:refresh:mergeChanges:
to the MOC causes the object to turn into a fault.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.