I watch the Core Data guides, and there are two way to obtain a new NSManagedObject instances.
- – initWithEntity:insertIntoManagedObjectContext: of NSManagedObject class
- + insertnewObjectForEntityForName:inManagedObjectContext: of NSEntityDescription class
Are there any difference between both methods? Or, they just mean the same thing for obtaining a new NSManagedObject under any conditions.
Based on what it’s said on the documentation, by using the class method from
NSEntityDescriptionto instantiate the NSManagedObject it’s possible to do it without declaring/importing its header. By setting the name of the class you will get back a "fully configured instance" of the object.It’s useful on early stages of development when things are changing constantly but it can be a risk factor since you don’t get any compilation errors or warnings if you misspell the name of your class, since it’s a string.
The method from
NSManagedObjectneeds that the interface of the specific class imported to your file, and make it more robust against errors, since the compiler can check if that class exist.For instance they will have the same result, they will return an instance of the specified class. Although the retain counts will be different:
- initWithEntity:insertIntoManagedObjectContext:(retain count == +1)+ insertnewObjectForEntityForName:inManagedObjectContext:(retain count == 0)Here it is the documentation
NSEntityDescription Class Reference (
insertNewObjectForEntityForName:inManagedObjectContext:)NSManagedObject Class Reference (
initWithEntity:insertIntoManagedObjectContext:)