The correct way to update an Objective-C project for Xcode 8 base data NSManagedObject subclass changes

I took a break from coding for several months and came back and discovered changes in CoreData using Xcode8 / iOS10 / macOS Sierra.

I'm trying to get a new generation of the NSManagedObject subclass in Objective-C, but there are very few on the Internet. I have a few things that I need to clarify before I start trimming my project and completely messing things up, but firstly, some things that I found shaking that could be useful to others there ...

Where are the things

Automatically generated files live deep in the DerivedData folder. Look at USER-> Library-> Developer-> Xcode-> DerivedData-> ProjectName-lotsOfRandomLetters-> Build, then continue to open folders until you find DerivedSources-> CoreDataGenerated.

Automatically generated files do not appear in the project folder or navigator, although if there is an error in one Xcode, you will see the source for you.

Xcode things generate

There are three configuration modes - manual / no, class definition and category / extension.

When the codegen entity is set to manual / no (which was the old behavior), creating a subclass of NSmanagedObject using Editor-> Create NSManagedObject Subclass generates 4 files inside your project ...

Entity + CoreDataClass.h and Entity + CoreDataClass.m and Entity + CoreDataProperties.h and Entity + CoreDataProperties.m

(previous version of Xcode 7 generated Entity.h, Entity.m, Entity + CoreDataProperties.h and Entity + CoreDataProperties.m)

If a class definition is specified for the codegen object, Xcode automatically generates the same 4 files in the derived data folder, and not in the project. These files are then marked with a comment saying that you are not modifying them.

Xcode generates 2 files if the codegen entities are set to Category / Extension. These files are marked with a comment saying that you are not modifying them. It...

Entity + CoreDataProperties.h and Entity + CoreDataProperties.m

These 2 files expect the Entity.h file to be in the project and will show an error in Xcode if it is missing. This is the time you can see the source for one of these files in Xcode.

What's in these files

The + CoreDataProperties files look the same as those generated by a previous version of Xcode generated files, with one addition. They contain all the attributes / properties for the object / NSmanagedObject and methods for processing objects that have a relationship from one to many or many. The new addition is the method for the fetchRequest subquery for the new fetchRequest NSmanageObject method.

Questions

1) Is class definition now an obvious and better choice for codegen when you do not have additional properties / functionality to add to a subclass of NSManagedObject, since it automatically updates files for you (when you save the project using cmd -s)?

2) The naming of files with + CoreDataClass follows the legend for the category in the class, which implies that there must be a class for this.

Do I believe that Entity + CoreDateClass.h / m files are a direct replacement for old Entity.h / m files? and that this is actually not a category, despite the file name?

3) For new NSManagedObject subclasses, should I import Entity + CoreDataClass.h, not Entity.h?

4) If I want to uncluttered my project by deleting most of my NSManagedObject subclass files, just delete the files in Xcode and set the codegen entities to the class definition or ...

Is there any magic under the hood that searches for an object + CoreDataClass when trying to #import entity.h, or will I need to go through and find all the links to #import entity.h and change them to #import entity + CoreDataClass.h?

5) Do I correctly assume that if I want to subclass NSManagedObject, where do I want to add the property and method that codegen should set to Category / Extension?

6) If I choose Category / Extension, I have to create my own NSmanagedObject subclass file, its just entity.h not entity + CoreDataClass.h?

7) If entity + CoreDataClass.h is the new accepted naming format for entity.h, why does the generated Category / Extension file look for a simple entity.h file instead of entity + CoreDataClass.h? Is this just an inconsistency in the Apples part and something that I should just accept or am I missing something I should know about?

Thanks.

+5
source share
1 answer

Good - quite a few people watched and did not receive answers, so I will try to answer it.

1) Yes - if you do not need to add additional properties / functions to the CoreData object, go to the class definition. This creates 4 files: Entity + CoreDataClass.h and Entity + CoreDataClass.m and Entity + CoreDataProperties.h and Entity + CoreDataProperties.m, but you will never see them because they are hidden from the visible place inside the derived data folder. If you need to check the attribute name, you can look in the main data editor, since you will not have access to these files.

2) Entity + CoreDateClass.h / m files are a direct replacement for old Entity.h / m files. Although you use file naming conventions for categories, they are not categories; do not let the Apple naming system confuse you. Browse the file and the class is defined as Entity not Entity + CoreDataClass.

3) For new NSManagedObject subclasses (with auto-generation with the "Class Definition" option), import Entity + CoreDataClass.h, not Entity.h. After all, this is the file you import, not the class defined internally. When using a class, its just Entity not Entity + ...

4) If you decide to advertise your project by deleting NSManagedObject subclass files, and then switching the codegen entities to "Class Definition", you will need to go through the project and change all the import statements that reference them by adding + CoreDataClass for the file name. Fortunately, this is not such a big deal as Xcode, it will still flag them as errors, so they are easy to find.

5) Yes - if you want to add properties or functionality to a subclass of NSManagedObject, use the Category / Extension option.

6) If you select Category / Extension, you need to create your own NSmanagedObject subclass file, name it Entity.h. Do not call it Entity + CoreDataClass.h, because the auto-generated Entity + CoreDataProperty.h object wants to import the Entity.h file.

7) Yes, this is just a naming inconsistency in the Apple part. Do not let him throw you like me.

And finally, do not forget ...

if you go down the path of using codegen → Category / Extension, if you add an additional relation to the entity, you will need to update the Entity.h file. For example, if you added a relation to a subclass of NSManagedObject called Car, you need to add @Class Car; in Entity.h.

+2
source

Source: https://habr.com/ru/post/1262175/


All Articles