Understanding NSManagedObject

和自甴很熟 提交于 2019-12-06 13:22:24

No, that is not the "right" approach. You can perform initialization of instance variables in awakeFromFetch. Apple guidelines for NSManagedObject subclasses include the following:

You are also discouraged from overriding initWithEntity:insertIntoManagedObjectContext:, dealloc, or finalize. Changing values in the initWithEntity:insertIntoManagedObjectContext: method will not be noticed by the context and if you are not careful, those changes may not be saved. Most initialization customization should be performed in one of the awake… methods. If you do override initWithEntity:insertIntoManagedObjectContext:, you must make sure you adhere to the requirements set out in the method description [...] (NSManagedObject Class Reference)

To really help, I'd need a deeper understanding of what you're trying to accomplish. Regardless, I strongly suggest combing through Apple's Core Data Programming Guide and sample code before proceeding.

I finally manage to solve this issue. Even if I am a newbie in objective-c, I think that introducing core data after the project is done, is not a good idea. Even if many claim it's easy. Unfortunately, all the people saying so, are showing as proof some really simple tutorial of one entity with one string attribute to change. Instead for my project I ended up writing much code in addition to the existing one, plus some subclassing (NSManagedObject for example) which break the original model. This added code has also to be written carefully. Derived problem can be as simple as an attribute not saved, or dangerous as deleting wrong entities.

Infact, my problem was due to a wrong configuration in decode and encode method in the classes involved in the process of serialization.

For my questions:

-Point one still remain unanswered, because I am not yet confident in objective-c

-Point two, as I said the related object had some problem with encode/code.

-Point three, I was wrong, there's a lot of code to write, depending how complex is the relevant class.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!