Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 496
Ludovic Avatar asked Jul 04 '16 18:07

Ludovic


3 Answers

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

like image 169
Vladimir Shutyuk Avatar answered Nov 08 '22 20:11

Vladimir Shutyuk


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!

like image 21
kev Avatar answered Nov 08 '22 19:11

kev


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

like image 9
oyalhi Avatar answered Nov 08 '22 20:11

oyalhi