开发者

NSManagedObject subclass woes

开发者 https://www.devze.com 2023-01-31 12:34 出处:网络
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.

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 calle开发者_如何学运维d during the objects life cycle?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消