Core data parent entity generated NSManagedObject subclass - ios

I have in my model a parent entity, "List" and a child entity (subentity) named "UserList" which is just a type of list. "UserList" has no attributes or relationships of its own, it inherits everything from List. List has a to-many relationship to ListItem.
I ask XCode 4.2 to generate the NSManagedObject subclasses.
For List it generates what I'd expect including,
- (void)insertObject:(NSManagedObject *)value inListItemsAtIndex:(NSUInteger)idx;
- (void)removeObjectFromListItemsAtIndex:(NSUInteger)idx;
...
However the generated "UserList" class does not declare any methods which would be ok if it was declared as a subclass of "List" but thats not what it generated.
#interface UserList : NSManagedObject
#end
Is UserList really a subclass of List? Can I just change the header? Or is something more complex going on?

A couple of things to check. First, make sure you have given both List and UserList entities a class name, and then make sure UserList has declared List as its parent entity.
The generator is finicky. If you have not specifically assigned a class name in the model GUI, it generates them on the fly. Unfortunately, it means that, depending on the order in which they are generated, some may not be generated with the right information (imaging UserList being generated first... if you did not specifically give List a class name, it only knows to use NSManagedObject).
By convention, I always specifically assign class names in the model GUI. I've not seen this happen when I specifically assign a class to each entity (under where you give the entity its name), but I guess it may happen.
Try to re-generate the class files. It may just get generated correctly this time.
EDIT
Clarification after comment...
NOTE: Unfortunately, Xcode is being very consistent in this matter. If you do not explicitly give class names to entities, others that reference it will be generated with "wrong" class names.
I suggest following one of following options for your work flow.
Always assign class names in the GUI for each entity.
Always generate the classes twice when you generate for the first time.
Use mogenerator https://github.com/rentzsch/mogenerator#readme instead of the Xcode class generator.

Related

Do I need to add a migration to Core Data if I change the entity class?

I have an entity in Core Data. In the configuration section of the model, the class for the entity is specifically defined as MyModule.MyEntityName. Now I want to change MyModule to a different name, such that the class will be MyOtherModule.MyEntityName. Can I just change it and it will be fine for those updating or will I need to add some sort of migration?
Things that affect the entity version are:
...the name of the entity, the version hash of the superentity (if present), if the entity is abstract, and all of the version hashes for the properties.
Changing the class name should be fine, but as always, make sure to test it yourself before releasing the code to users.

Create NSManagedObject Subclass from Core Data Data Model in Xcode 6

I've got two entities in a Core Data Data Model. There is a one to many relationship between them. When I navigate to Editor --> Create NSManagedObject Subclass..., my models are generated but I do not get strongly typed method signatures like - (void)addPhotosObject:(NSManagedObject *)value; on the many side of the relationship.
According to what I've read, in Xcode 5 at least, this is because the class on the one side of the relationship was created first so the class on the many side of the relationship does not yet exist. The suggestions I see say to to simply Create NSManagedObject Subclass... again and the method should generate methods with strongly typed classes but the method signatures are not changing to strongly type.
Is there a trick to getting strongly typed method signatures?
After deleting the NSManagedObject subclasses that were initially created by Create NSManagedObject Subclass... and simply re-generating them by selecting Create NSManagedObject Subclass... again, the method signatures ended up strongly typed which gave me a Photo as a parameter like this - (void)addPhotosObject:(Photo *)value; instead of an NSManagedObject.

Core Data - Fetched Property with bitmasks and abstract entities

(Data models renamed to preserve anonymity. :D )
So, I have a situation where on a Thing, whose configuration is defined by its own special object, though that object can be one of 2 class types.
I would like to be able to do Fetch Requests on this model by asking if the Thing's Configuration is of a certain known subclass, and if so, does it have a few specific flags set in its bitmask. If this is true, I define this as being "not special". Therefore, if it also has the other type of subclass as it's Configuration, it is also "not special".
My questions are these:
Have I got my syntax right? (In my app I get faults)
Why does it want to return an NSArray? (I thought its traversing a to-one relationship on a single object. How would I therefore interpret this NSArray?)
I should add that I only know Core Data via MagicalRecord more or less. And I use mogenerator too.

Core Data modeler not updating when entity names change

I am having an issue using Core Data modeler. I had an issue where I needed to change the names of entities in the modeler. When I did so, I deleted the associated managed object subclasses, did a clean, then went back the modeler, highlighted the entities with the new names, then generated subclasses using
Editor > Create NSManagedObject Subclass
When I did so, the newly generated subclasses still have the original names. Am I missing something?
In the model editor, you set the class name separately from the entity name. They don't have to be the same. If you only change the entity name name, what you're seeing is normal.
Select the ENTITY you want to change
Select Data Model from right hand top corner as shown below, and change as u want. Entity name and Class no need to be the same.
Finally Clean and Build the project.

Is there a way to not need to regenerate the model with Core Data iOS

Say I generated an entity (User) in Model.xcdatamodeld with attributes firstName,lastName, dateCreated and so on. And then I generated a User.h, User.m
Then later on I realize I forgot to put an extra attribute into the User model.
Right now, everytime I have to delete User.h and User.m when I add a new attribute because somehow the compiler doesn't recognize the new attribute I put in.
Is there a way to note delete the generated files?
If you are just adding a new attribute, just add a new property to the files you are generating, and add #dynamic propertyName on the implementation file.
On an unrelated note: make sure you are assigning class prefix to the classes you are generating as NSManagedObject subclasses from your model. "User" might be a system class, and you will get random runtime error messages and crashes.

Resources