NSManagedObject subclass outside of managed object as a normal object

后端 未结 2 1481
梦毁少年i
梦毁少年i 2021-02-05 17:49

I have an entity object Country with country name and country code. It is a subclass of NSManagedObject and I am using it with core data model to store its value to a persistent

相关标签:
2条回答
  • 2021-02-05 17:56

    2nd paragraph of the NSManagedObject class documentation's overview:

    A managed object is associated with an entity description (an instance of NSEntityDescription) that provides metadata about the object (including the name of the entity that the object represents and the names of its attributes and relationships) and with a managed object context that tracks changes to the object graph. It is important that a managed object is properly configured for use with Core Data. If you instantiate a managed object directly, you must call the designated initializer (initWithEntity:insertIntoManagedObjectContext:).

    From the documentation of the method:

    Important: This method is the designated initializer for NSManagedObject. You should not initialize a managed object simply by sending it init.

    The documentation is actually very good.

    You do not want to try to use an NSManagedObject outside of a viable CoreData stack. NSManagedObjects are quite explicitly designed to work within a correctly configured Core Data environment. If you need a temporary instance, you can either create an in-memory store or create one in your regular store and just don't save the changes without deleting it first.

    0 讨论(0)
  • 2021-02-05 18:10

    Use initWithEntity:insertIntoManagedObjectContext: and pass nil for managed object context.

    0 讨论(0)
提交回复
热议问题