2

I'm trying to understand how Core Data works in Objective-C and can't quite get the purpose of categories that have the name SomeClass+CoreDataClass and are created when we want to subclass NSManagedObject.

As far as I know, they should be created only once and not regenerated every time we need to update our entity's structure, so we can add our methods there. However, they are recreated as blank files every time I regenerate a subclass of NSManagedObject.

I think, I'm missing something, so could you explain their purpose?

Tigran Iskandaryan
  • 1,371
  • 2
  • 14
  • 41
  • What sense would the file make when it's created the first time, and then after changes you make to the model be out of date? It would be necessary to manually sync up the file with the model, which makes automatic generation of the file not much help. You can extend the created class in your own file, or make Xcode generate an extension instead of a class, so you can write a class where the given file adds the pieces defined in the model. – Fabian Aug 24 '18 at 16:52
  • Try [this](https://stackoverflow.com/a/40647786/3985749) and/or [this](https://stackoverflow.com/a/41276733/3985749). – pbasdf Aug 25 '18 at 07:36
  • @Purpose, I was meaning, let's say we want to add our custom methods to those model files. If they become regenerated every time with new attributes (which are properties inside them), we will lose all our code. I know that for that, we can create our custom categories, but I can't understand the reason why Xcode creates those files, which are basically empty – Tigran Iskandaryan Aug 26 '18 at 09:10
  • @TigranIskandaryan You can disable auto-generation of the file based on the model, however many use auto-generation so they do not have to add a method in their files too when they change the `.xcdatamodeld`. However you usually don’t add custom methods to said auto-generated file for the obvious reasons, but rather use class extensions to extend the auto-generated file from another file you create, which does not get changed because you create it in your own folder. – Fabian Aug 26 '18 at 09:43
  • @Purpose, Those files basically give us a class based on which we can create our categories and extensions, but we never add our methods there because the code won't persist the model changes. Am I right? – Tigran Iskandaryan Aug 26 '18 at 12:58
  • @TigranIskandaryan It not just gives us a class on which we add methods, functions, but it rather gives you a class which has all the attributes which you create in your `.xcdatamodeld`, and if you change your `xcdatamodeld`, it automatically updates said class to reflect the model, without you having to keep it in sync. That’s the purpose of auto-generating said class, that you do not have to keep your CoreDataModel in sync with the `NSManagedObject`-derived class. You can shut off this auto-syncing, but then you have to keep them in sync yourself manually. – Fabian Aug 26 '18 at 14:05
  • @Purpose, thanks for the explanation. But shouldn't I regenerate the subclasses of `NSManagedObject` after updating the model? I mean, when I add attributes to it, they won't appear in the subclass automatically. I should recreate those files again, or am I wrong? – Tigran Iskandaryan Aug 27 '18 at 09:22
  • It should usually generate and update them for you if `Class Definition` or `Category/Extension` is selected for the given Model class, see more on it [here](https://medium.com/@kahseng.lee123/core-data-codegen-explained-462c30341041). If Xcode didn’t you may try `Clean code` to delete generated files so Xcode regenerates them. – Fabian Aug 27 '18 at 09:27
  • @Purpose, the codegen is set to `Manual/None` because otherwise I get errors after subclass are generated saying that the files already exist. – Tigran Iskandaryan Aug 30 '18 at 16:02
  • @TigranIskandaryan Two files can‘t have the same name in the same target. Just name yours different than the one being generated automatically. – Fabian Aug 30 '18 at 16:04
  • @Purpose, yes but I don't have other class or file named `SomeClass`. That's for sure. I remember the same was happening for Swift, too (last time I tried Core Data on Swift that was 3.x). For that reason I was using `Manual/None` but there was clear which file would be regenerated. But again, I'm pretty sure there is no file with the same name in the target. – Tigran Iskandaryan Aug 30 '18 at 16:10
  • `SomeClass+CoreData.swift` is always being generated by Xcode, just leave off `+CoreData` on your file. If that doesn‘t help Build clean and try again. If that doesn‘t help either I‘m out of ideas. – Fabian Aug 30 '18 at 16:12
  • @Purpose, for now I'll create categories of the class. Anyway, thanks for all explanations! – Tigran Iskandaryan Aug 30 '18 at 16:16

1 Answers1

0

+CoreDataClass is generated by xcode, you shouldn't edit it, because you can loose changes. You can edit SomeClass, Xcode will only create it if it isn't exist. This division was made just for programmers convenience. You can freely move code from +CoreDataClass to main file and delete category if you wish.

Cy-4AH
  • 4,370
  • 2
  • 15
  • 22
  • When I generate subclasses I don't get `SomeClass` as is. There are four files generated: `+CoreDataClass` and `+CoreDataProperties` with `h` and `m` each. Right now, to make code persist I create a category of 'SomeClass'. Maybe that's because the codegen is set to Manual/None because otherwise I get errors after subclass are generated saying that the files already exist. – Tigran Iskandaryan Aug 30 '18 at 15:59