Most (all I’ve seen) Core Data tutorials use the following code snippet with @"MyEntityClass" hard-coded in:
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"MyEntityClass"];
Is it safe to use NSStringFromClass() as an Entity Name?
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([MyEntityClass class])];
This seams to be much easer to deal with regarding refactoring and the like. Especially since I am having Xcode create my NSManagedObject subclasses. I ask because I have never seen this before, so perhaps I am missing something.
Yes, that code is fine, if your entity’s class is set to
MyEntityClassin your model.I prefer to give the entity class a class method that returns the entity name:
and call it like this:
This way, if I want to change the class name without changing the entity name in the model, I can just make the change in the class method:
Why would I do that? Well, I might decide on a better name for the entity. Changing the class name doesn’t break compatibility with an existing Core Data persistent store, but changing the entity name in the model file does. I can change the class name, and the
entityNamemethod, but leave the entity name unchanged in the model, and then I don’t have to worry about migration. (Lightweight migration supports renamed entities so it’s not that big of a deal either way.)You could go further and actually have the
entityNamemethod look up the entity name from the managed object model at runtime. Suppose your application delegate has a message that returns the managed object model:Obviously, if you really want to do this, you should factor out the contents of the
dispatch_onceblock into a helper method, probably on your app delegate (or wherever you get the model).