Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSManagedObject subclass outside of managed object as a normal object

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 store.

I have a place where the same Country object will used as a normal object i.e. I will use it to store some temporary country name.

For that I have initialized the Country as following

[NSManagedObject alloc] init]

Initialization successfully done, but I am not able to set any property to that object. Hence I did an exploration. In that I found that, init for the NSManagedObject is not supported as per the documentation.

I don't know how to use the NSManagedObject Country with CoreData as well as a normal Object.

like image 653
raksja Avatar asked Sep 02 '09 06:09

raksja


People also ask

How do I create a subclass in NSManagedObject?

From the Xcode menu bar, choose Editor > Create NSManagedObject Subclass. Select your data model, then the appropriate entity, and choose where to save the files. Xcode places both class and properties files into your project.

What is NSManagedObject in Core Data?

An NSManagedObject instance implements the basic behavior required of a Core Data model object. The NSManagedObject instance requires two elements: an entity description (an NSEntityDescription instance) and a managed object context (an NSManagedObjectContext instance).

Can we have multiple managed object contexts in Core Data?

Most apps need just a single managed object context. The default configuration in most Core Data apps is a single managed object context associated with the main queue. Multiple managed object contexts make your apps harder to debug; it's not something you'd use in every app, in every situation.

How do I update my class in NSManagedObject?

You can do this: Choose "Create NSManagedObject Subclass…" from the Core Data editor menu to delete and recreate this implementation file for your updated model. You will then remove the files you already had, and the new ones will be created.


2 Answers

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.

like image 164
bbum Avatar answered Sep 29 '22 13:09

bbum


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

like image 35
malhal Avatar answered Sep 29 '22 11:09

malhal