Ludovic
Ludovic

Reputation: 2002

Duplicate files in DerivedData folder using CoreData generator

I'm trying to generate NSManagedModels from my datamodel. Generation works but after I got many errors :

error: filename "Station+CoreDataProperties.swift" used twice: '/Users/Me/MyApp/Models/CoreData/Station+CoreDataProperties.swift' and '/Users/Me/Library/Developer/Xcode/DerivedData/MyApp-gwacspwrsnabomertjnqfbuhjvwc/Build/Intermediates/MyApp.build/Debug-iphoneos/MyApp.build/DerivedSources/CoreDataGenerated/Model/Station+CoreDataProperties.swift' :0: note: filenames are used to distinguish private declarations with the same name

I try clean build folder and derivedData directory hard delete. I'm using Xcode 8 BETA maybe it's a bug ?

Upvotes: 43

Views: 10038

Answers (7)

Toastor
Toastor

Reputation: 8990

For the sake of completeness..:

I just ran into the same error, but none of the proposed solutions worked. What puzzled me was that even switching from automated code generation to manual for the one (as I thought) problematic entity didn't do anything.

Finally, I figured out that I had several entities with the same name, but they all shared the same classname. The reason for this was that I copy&pasted one entity several times to save me some work, because they also have a few attributes in common.

Turns out XCode renames the duplicates by adding 1, 2,... to the entity name, but leaves the class name as before. And since now entity name and class name are "unrelated", renaming the entity won't change the class name either.

Hope it helps someone - I have also filed a bug report for this.

Upvotes: 0

100grams
100grams

Reputation: 3510

If you generated CoreData subclasses with codegen: ClassDefinition your basically screwed. The only way to fix it is to:

  1. Delete your CoreData subclasses.
  2. Delete your derived data folder.
  3. Clean your project (CMD+K).
  4. Generate new CoreData subclasses, this time select Codegen: Manual/None and Module: Current Product Module

Upvotes: 5

kev
kev

Reputation: 2346

This post greatly helped me solve this problem myself. Personally I look at this as an Xcode bug. Bug or not this is a huge chicken and egg situation.

I ran into this by:

  1. Created a new Project using Core Data
  2. Generated my NSManagedObject subclass+extension (while codegen: ClassDefinition)
  3. I accidentally saved the generated classes in the Wrong folder
  4. I deleted the generated files
  5. Re-generated in folder I wanted
  6. đź’Ą- Xcode used twice errors

As others have posted I kept cleaning my build (and clean build folder) but that never fixed the build issue.

I finally figured out if you originally created your NSManagedObject generated classes with codegen: ClassDefinition, as I did without knowing then you are locked in for the chicken and egg issue.

I then deleted the auto generated classes thinking I had to re-generate, so I did. Once re-generated I would get the used twice build error again. I manually went into the ../DerivedSources/CoreDataGenerated/Model/.. and deleted the duplicates. Again, I re-generated thinking I'd only have 1 copy (in my project) but I was wrong. If codegen: ClassDefinition was originally set then Xcode will keep creating the auto-generated classes+extensions and put them in the buried folder ../DerivedSources/CoreDataGenerated/Model/... I repeated this chicken and egg a few times before catching on.

I later realized you do indeed need to mark codegen: Manual/None however to get things back in sync you need to delete the auto-generated files in ../DerivedSources/CoreDataGenerated/Model/.. and in your project if you have any there still.

Be careful setting codegen: Manual/None, for me it was bit tricky because codegen: Manual/None wouldn't stick. I had to click back and forth between entities multiple times to double/triple check each entity was set to codegen: Manual/None. Then auto generate the files. At this point your only copy of the auto generated files should be in your project and not in ../DerivedSources/CoreDataGenerated/Model/...

Last, I think this is a bug because if you specify codegen: Manual/None I don't expect Xcode to auto generate files at all, yet it does and puts them in your project. More confusing if your setting is codegen: ClassDefinition, who the heck knows Xcode will put the files in a buried directory yet it is available for use in your project. My beef with this is the auto generated files aren't source controlled and if I change computer I have to know to auto-generate them on the new station.

Hope this helps someone else!

Cheers!

Upvotes: 11

Vladimir Shutyuk
Vladimir Shutyuk

Reputation: 2976

I get this in Xcode 8.1 For me following steps solved the issue. Please note that order matters.

1) Create entity in Core Data model.

2) Under class section, make settings as on following image.

Module: Current Product Name

Codegen: Manual/None

3) Generate your NSManagedObject subclass.

enter image description here

Upvotes: 79

Jacob F. Davis C-CISO
Jacob F. Davis C-CISO

Reputation: 890

After following the guidance from oyalhi and Vladimir Shutyuk, (deleting the NSManagedObject files, changing the entity codegen to Manual/None), I had to restart Xcode to allow it to index again before I could re-generate the NSManagedObject files and get a successful compile.

Upvotes: 0

oyalhi
oyalhi

Reputation: 3984

This is indeed not a bug. As @Morrowless suggests both class definition and properties extension are created. If this is not wanted, select Manual/None under Codegen before generating the code. If the code is already generated, just delete them, and try Editor->Create NSManagedObject Subclass... again from the menu (after setting Manual/None).

Note, in the picture below, the Class Name 'Contact' is specific to my project. You will see your entity name instead.

enter image description here

Upvotes: 9

Morrowless
Morrowless

Reputation: 6958

This is not a bug. Codegen generates these files in the DerivedData folder, so you don't need to create them again in your project, hence the compile error.

From Xcode 8.0 Release notes:

Xcode automatically generates classes or class extensions for the entities and properties in a Core Data data model. Automatic code generation is enabled and disabled on an entity by entity basis, and is enabled for all entities in new models that use the Xcode 8 file format. This feature is available for any data model that has been upgraded to the Xcode 8 format. You specify whether Xcode generates Swift or Objective-C code for a data model using the data model’s file inspector.

When automatic code generation is enabled for an entity, Xcode creates either a class or class extension for the entity as specified in the entity's inspector: the specified class name is used and the sources are placed in the project’s Derived Data. For both Swift and Objective-C, these classes are directly usable from the project’s code. For Objective-C, an additional header file is created for all generated entities in your model. The header file name conforms to the naming convention “DataModelName+CoreDataModel.h”.

However, if you selected Category/Extension under the codegen pulldown menu in the data model inspector (because you want to add logic to your model): codegen will wrongly generate both the class definition and properties extension.

The solution is to simply delete the properties extension (ClassName+CoreDataProperties.swift). Your project should now compile.

Upvotes: 3

Related Questions