Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xcode 7 generates core data entity with additional CoreDataProperties category

I have strange new feature in xcode 7, when I generate new NSManagedObject subclass then xcode create two classes: entity and their CoreDataProperties category, which contain full implementation. In picture below an example what I mean.

enter image description here

I cannot find any documented info about this, who can explain why it works so

like image 506
Yevgeniy Logachev Avatar asked Oct 13 '15 15:10

Yevgeniy Logachev


People also ask

What is Nsfetchrequest in Swift?

A description of search criteria used to retrieve data from a persistent store.

What is codegen in Core Data?

Class DefinitionThis configuration is the default Codegen configuration when you create an entity in the data model editor. With this configuration, Xcode will automatically generate the required NSManagedObject subclass as part of the project's derived data.

How do I make a NSManagedObject subclass in Xcode 13?

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.


2 Answers

I just noticed this and also could not find any documentation about it but I've experimented with this new feature and it works like this. When you first generate NSManagedObject subclass from your Core Data model then Xcode will generate 4 files:

DBUser.h

#import <Foundation/Foundation.h> #import <CoreData/CoreData.h>  NS_ASSUME_NONNULL_BEGIN  @interface DBUser : NSManagedObject  // Insert code here to declare functionality of your managed object subclass  @end  NS_ASSUME_NONNULL_END  #import "DBUser+CoreDataProperties.h" 

DBUser.m

#import "DBUser.h"  @implementation DBUser  // Insert code here to add functionality to your managed object subclass  @end 

DBUser+CoreDataProperties.h

#import "DBUser.h"  NS_ASSUME_NONNULL_BEGIN  @interface DBUser (CoreDataProperties)  @property (nullable, nonatomic, retain) NSNumber *id; @property (nullable, nonatomic, retain) NSString *name;  @end  NS_ASSUME_NONNULL_END 

DBUser+CoreDataProperties.m

#import "DBUser+CoreDataProperties.h"  @implementation DBUser (CoreDataProperties)  @dynamic id; @dynamic name;  @end 

So as you can see now all properties are in a separate file with category (CoreDataProperties). Later if you generate NSManagedObject subclass for the same model Xcode 7 will regenarete only 2 files with category (DBUser+CoreDataProperties.h and DBUser+CoreDataProperties.m) to update all properties from your model but it will not make any changes to 2 other files (DBUser.h and DBUser.m) so you can use these 2 files to add there some custom methods or properties etc.

In previous version Xcode generated always only 2 files (DBUser.h and DBUser.m) and it put properties there so you could not easily modify these files because your custom implementation was deleted everytime you regenerated your subclasses. Therefore it was a common practice to manually create a category and put your methods in your category which was oposite to what we can see in Xcode 7. That however had many disadvantages because we had to use a category for implementation of our methods which does not allow to do certain things and now we can easily modify the main interface and implementation files which allows us to do anything with it. Hurray!

like image 189
Leszek Szary Avatar answered Oct 06 '22 01:10

Leszek Szary


Previously the generated code went into a EntityName.h and EntityName.m that you had to "extend" with an interface such as EntityName+Create.h and EntityName+Create.m.

This was hard to understand for beginners who often modified the EntityName.m class and lost their code.

Now it is in the right way: the code generator will not erase the existing code.

The other answers are very good at explaining the new system.

But nobody talks about the new compatibility issue if you have entities based on the old system.

My solution: I still put my own code in EntityName+Create.m, but in EntityName+Create.h I refer to EntityName+CoreDataProperties.h instead of just EntityName.h (I emptied the previously generated code in EntityName.h and EntityName.m). This solution avoided me to move my code from EntityName+Create.m and to change all references to EntityName+Create.h.

I hope this helped you.

like image 34
Nicolas Gros Avatar answered Oct 06 '22 01:10

Nicolas Gros