Can I manually create an instance of the Core Data Entity class for temporary use?

I have a custom class for a Core Data object called Friends. When I parse an XML file, I need to create temporary instances to store temporary data, without using Core Data at this point. Thus, there are two options:

A) create an NSMutableDictionary to store temporary data when parsing the "object" from XML.

B) Use an object class to store temporary data during parsing. I would prefer it because it is clearer and more DRY to me, since the data structure is already declared there, and I would not have to duplicate the entire data model of this object in NSMutableDictionary.

But B has a problem: by default, all @dynamic properties are, and as far as I know, Core Data takes care of creating implementations at runtime. Therefore, I can’t just use the properties there. Thus, the question arises whether it is worth spending on this and it is even possible to change this class so that it can be used without Core Data as a temporary object of a data container, i.e. By creating instance variables. Of course, if I had to create a dictionary for storing temporary data, that would not make any sense, and I would just go with A.

+3
source share
2 answers

UPDATE: It looks like you can use a null context (see Adam's answer below)

, - .

(NSMutableDictionary) - , setValuesForKeysWithDictionary: .

, , , . , .
. , , , ( ) , , .

+2

gerry3, Apple docs . nil:

- (ID) initWithEntity: (NSEntityDescription *) insertIntoManagedObjectContext: (NSManagedObjectContext *)

... , [context insertObject: self] ( awakeFromInsert ).

, ( temp NSManagedObjects - ).

+2

Source: https://habr.com/ru/post/1732515/


All Articles