26 Replies
      Latest reply on Jan 11, 2017 4:09 PM by gcortes
      GJNilsen Level 1 Level 1 (0 points)

        When I add new entities, they dissappear after a while from the .xcdatamodeld file. Further, Xcode sometimes generate a third file when creating the NSManagedObject Subclass, containing some kind of macro that crashes the build. It looks like it is a template issue gone bad.

        I have submitted rdar://2681978

        • Re: Having problems with Core Data?
          KMT Level 9 Level 9 (14,865 points)

          Anything relevant in the release notes?

           

           

          Core Data

            • For an attribute that allows specification of a custom class in its inspector, such as a transformable attribute, there is no way to specify the module or header from which to import that
              class. (25669424)

           

            • When using Core Data automatic code generation, changes made to a data model are not immediately visible to other code in the project.

              Workaround: Building the project again updates the generated code. (There is no need to clean the project before building it again.) (25789848)

           

            • Generated managed object classes are may not be created before the first build. (26564209). Workaround: Perform a clean build.

           

            • Re: Having problems with Core Data?
              GJNilsen Level 1 Level 1 (0 points)

              Happens even after multiple builds.

                • Re: Having problems with Core Data?
                  KMT Level 9 Level 9 (14,865 points)

                  Even when doing an option-clean build folder?

                   

                  Use Xcode's Product menu, then, with the option key pressed, select 'Clean Build Folder'. Confirm no errors in the navigator and go again.

                    • Re: Having problems with Core Data?
                      GJNilsen Level 1 Level 1 (0 points)

                      Ofcoz the build folder was cleaned, and no errors in the navigator. The entities just dissappear. Further, some times a file called ___COREDATA_DATAMODELNAME___+CoreDataModel.swift shows up, just containing the Core Data NSManagedObject subclass template, with the project name, date, my name and organization in it. The rest is just placeholder text.

                        • Re: Having problems with Core Data?
                          KMT Level 9 Level 9 (14,865 points)

                          >Ofcoz the build folder was cleaned,

                           

                          I asked if it was option-cleaned, not just cleaned.

                          • Re: Having problems with Core Data?
                            Robb18 Level 1 Level 1 (0 points)

                            I'm having the same issue.  I opt-cleaned too.

                              • Re: Having problems with Core Data?
                                dndydon Level 1 Level 1 (0 points)

                                I'm having the same issue, with opt-cleaning, and trying this:  Xcode 8.0 beta 3 (8S128d)

                                1. shift-command-opt-clean build folder

                                2. select .xcdatamodeld file and then set minimum tool version in File Inspector to Xcode 7.3

                                3. shift-command-opt-clean build folder

                                4. quit Xcode, relaunch

                                5. select .xcdatamodeld file then "create NSManagedObject subclasses" in Editor menu

                                6. choosing the model, entities (there are two), and group folder

                                7. Xcode autogenerates a bunch of files, including the __COREDATA_DATAMODELNAME__+CoreDataModel.swift file

                                8. I've learned through repetition that builds will not work, and I have taken several branches from here.

                                9. I've tried unsuccessfully to opt-clean again and rebuild or regenerate as 7.3 and 8.0.  I've tried unsuccessfully to just regenerate 7.3 and 8.0 to see if it cleans up by itself. I've also tried to edit all the files in question so they are dirty (or clean).

                                 

                                All results are the same...  Same files generated, I'm also looking at the contents of my Derived Data folder to see what comes and goes.

                                 

                                I'm wondering if I could manually rename the files with prefix dot.names that cfhgsfc587  described. Would that fix things? I doubt it.

                                 

                                I'm stuck. Can't build. I guess I'll work on something else for a while.

                                  • Re: Having problems with Core Data?
                                    dndydon Level 1 Level 1 (0 points)

                                    Just a bit more detail on the errors I get when trying to build the project:

                                    <unknown>:0: error: filename "DSApp+CoreDataClass.swift" used twice: '/Users/donsleeter/Developer/iOSDev/Krell/DSSFApp/DSSFApp/DSApp+CoreDataClass.swift' and '/Users/donsleeter/Library/Developer/Xcode/DerivedData/DSSFApp-gxlkmcisruheozcdvvtfcuhqsyrr/Build/Intermediates/DSSFApp.build/Debug-iphonesimulator/DSSFApp.build/DerivedSources/CoreDataGenerated/DSSFApp/DSApp+CoreDataClass.swift'

                                    <unknown>:0: note: filenames are used to distinguish private declarations with the same name

                                    <unknown>:0: error: filename "DSApp+CoreDataProperties.swift" used twice: '/Users/donsleeter/Developer/iOSDev/Krell/DSSFApp/DSSFApp/DSApp+CoreDataProperties.swift' and '/Users/donsleeter/Library/Developer/Xcode/DerivedData/DSSFApp-gxlkmcisruheozcdvvtfcuhqsyrr/Build/Intermediates/DSSFApp.build/Debug-iphonesimulator/DSSFApp.build/DerivedSources/CoreDataGenerated/DSSFApp/DSApp+CoreDataProperties.swift'

                                    <unknown>:0: note: filenames are used to distinguish private declarations with the same name

                                    <unknown>:0: error: filename "Location+CoreDataClass.swift" used twice: '/Users/donsleeter/Developer/iOSDev/Krell/DSSFApp/DSSFApp/Location+CoreDataClass.swift' and '/Users/donsleeter/Library/Developer/Xcode/DerivedData/DSSFApp-gxlkmcisruheozcdvvtfcuhqsyrr/Build/Intermediates/DSSFApp.build/Debug-iphonesimulator/DSSFApp.build/DerivedSources/CoreDataGenerated/DSSFApp/Location+CoreDataClass.swift'

                                    <unknown>:0: note: filenames are used to distinguish private declarations with the same name

                                    <unknown>:0: error: filename "Location+CoreDataProperties.swift" used twice: '/Users/donsleeter/Developer/iOSDev/Krell/DSSFApp/DSSFApp/Location+CoreDataProperties.swift' and '/Users/donsleeter/Library/Developer/Xcode/DerivedData/DSSFApp-gxlkmcisruheozcdvvtfcuhqsyrr/Build/Intermediates/DSSFApp.build/Debug-iphonesimulator/DSSFApp.build/DerivedSources/CoreDataGenerated/DSSFApp/Location+CoreDataProperties.swift'

                                    <unknown>:0: note: filenames are used to distinguish private declarations with the same name

                                    **Command /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc** failed with exit code 1

                          • Re: Having problems with Core Data?
                            benguild Level 1 Level 1 (0 points)

                            In my experience, the first line of your generated protocol/extension header is:

                            #import "YourClass.h"
                            
                            

                             

                            You can put your imports there... and they'll be imported into your protocol/extension as well.

                          • Re: Having problems with Core Data?
                            nerdtypething Level 1 Level 1 (0 points)

                            I'm having the same problems generating NSManagedObject subclass for my entities. I have attempted the opt-clean build -> build -> generate NSManagedObject subclass. I get the third __COREDATA_DATAMODELNAME__+CoreDataModel.swift file generated that throws two compiler errors. Further the subclass files themselves throw "Invalid redecleration of '<classname>'"

                            • Re: Having problems with Core Data?
                              nerdtypething Level 1 Level 1 (0 points)

                              I have more information. After looking at the build scripts I noticed there is a step that says "Generate code for data model <ProjectName>.xcdatamodeld." It turns out this utility is auto-generating all the NSManagedObjectModel subclasses for every Entity that is defined in your data model and dumping them into the DerivedData folder of your project.

                               

                              The later step "Compile Swift source files" is bringing in these NSManagedObjectModel subclasses found in the DerivedData folder for compilation.

                               

                              I looked in my target build settings and could find nothing that indicates that 1) Xcode 8 should auto-generate my entire Core Data model or 2) to compile files found in DerivedData.

                               

                              For verification I attempted to instantiate one of my Entities' NSManagedObjectModel subclass in a view controller file and the code complete was able to find the class. I can access all the data model properties as one might expect.

                               

                              I am not sure if this is new designed behavior in Xcode 8.

                               

                              I created a Core Data project in Xcode 7 and was unable to see this "Generate code for data model" compile step.

                              • Re: Having problems with Core Data?
                                nerdtypething Level 1 Level 1 (0 points)

                                This will teach me to read the beta release notes more carefully:

                                 

                                "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 using 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 file name conforms to the naming convention

                                'DataModelName+CoreDataModel.h'."

                                 

                                Working as intended. Don't manually generate NSManagedObjectModel subclasses in new data models created in Xcode 8.

                                  • Re: Having problems with Core Data?
                                    malhal Level 1 Level 1 (20 points)

                                    nerdtypething, I would suggest watching this video to learn about the new auto subclass generation:

                                    https://developer.apple.com/videos/play/wwdc2016/242/

                                     

                                    When I do try and generate subclasses manually I also get the bug where it generates a file called __COREDATA_DATAMODELNAME__. I'm also experiencing a bug where choosing codegen Manual/None has no effect, the files are still generated in the derived folder and the model I excluded is added to the model umbrella header. This is only beta 1 after all.

                                      • Re: Having problems with Core Data?
                                        nerdtypething Level 1 Level 1 (0 points)

                                        Thank you. I also am finding that choosing the codegen Manual/None is having no effect. It's quite crippling for unit testing since I can't expose any of my methods that take my managed object subclasses as parameters as public since the subclasses are all marked as internal and no way for me to make them otherwise.

                                      • Re: Having problems with Core Data?
                                        fareast55 Level 1 Level 1 (0 points)

                                        Super helpful. Thanks. I trashed my Data Model, created a new one and in the file inspector for the model set it to Objective C.

                                         

                                        From there, I just used Create NSManagedObject subclass in Editor. That added the files as extensions to my project. Simply import the one for the class file and that did 'er.

                                         

                                        Thanks again!

                                      • Re: Having problems with Core Data?
                                        nerdtypething Level 1 Level 1 (0 points)

                                        Okay, after watching the video linked to by malhal I found a work-around for Beta 1 that solves the ownership of the NSManagedObject subclass problem. By this I mean being able to manually generate the subclass and your project being in control of the primary class definition.

                                         

                                        In the core data model select the Entity you would like to control. Select the Data Model Inspector. Under the "Class" category there is a Codegen drop-down. Selecting "Manual/None" right now still auto-gens it (seems to be a bug). But if you select "Category/Extension" it will only create the properties/relationships definitions in an internal extension file stored in the DerivedData folder.

                                         

                                        Now manually generate the entity subclass. There is yet another bug here that generates those three files: the main class definition, the extension for properties and relationships and that weirdo file with the single macro defined. Simply delete all the files except the <Class>+CoreDataClass.swift file and everything should be okay.

                                         

                                        In my case now that I own the primary class definition I am free to make the class public and, thus, expose my local data manager class methods publicly to my unit tests.

                                          • Re: Having problems with Core Data?
                                            MotivDev Level 1 Level 1 (0 points)

                                            Thanks nerdtypething!

                                             

                                            This got me going on the right track.  I did have to edit the xml file for the data model to finally make it so I could build my project without getting the auto generated object files.  I added 'minimumToolsVersion="Xcode 7.0"' in <model> section of the file and things are now working as expected.  I found that no matter what I tried, I could not get it to leave Code Gen to anything other than "Class Definition" when I opened the project again and it would continue to fail when trying to compile. 

                                             

                                            This appears to be a bug with XCode....  did you file a bug report on your finding?

                                             

                                            Rich

                                          • Re: Having problems with Core Data?
                                            cfhgsfc587 Level 1 Level 1 (0 points)

                                            I have a similar but not identical problem.

                                             

                                            Whatever method I select in the Codegen pop-up for my data model entities, I get a set of swiftc errors of the following form for each entity:

                                             

                                            <unknown>:0: error: no such file or directory: '/Users.../DerivedSources/CoreDataGenerated/Library/.AppState+CoreDataClass.swift'
                                            <unknown>:0: error: no such file or directory: '/Users/.../DerivedSources/CoreDataGenerated/Library/.AppState+CoreDataProperties.swift'
                                            

                                             

                                            Note the dot prepended to each of the names of the generated files.vAnd indeed, if I go to the indicated directory, none of the generated files have this prepended dot.

                                             

                                            So it seems there's a mismatch between the names generated by whatever tool is creating these files, and whatever tool is trying to process these files.

                                             

                                            Additionally, if I try to generate the entity classes/extensions manually (in the prior, usual manner), Xcode crashes immediately after selected "File ->New...->File...->NSManagedObject subclass".

                                             

                                            Carl

                                              • Re: Having problems with Core Data?
                                                cfhgsfc587 Level 1 Level 1 (0 points)

                                                I'm still having this problem with beta 2. Has anyone else encountered this particular issue? Does anyone have any idea how I might be able to fix it?


                                                The core of the problem is that while Codegen is creating the right files, and putting them in the CoreDataGenerated folder, none of the generated files have a dot (".") prefixed to their name, which the compiler expects them to have.

                                                 

                                                The expectation of this "." seems to arise from the datamodel's "Default" configuration, which for each entity (e.g. AppState) specifies a Class of the same name with the prepended dot (".AppState"). Even editing this configuration to remove the "." before each class name makes no difference: the same clases are generated, but the compiler still looks for a class with a prepended dot.

                                                 

                                                I notice, too, that when I do edit the class names in the configuration to remove the dot, the class module for each enitity in the datamodel editor becomes (grayed-out) "Global namespace"; whereas if I select instead "Current Project Module", the dots all reappear in the configuration class names.

                                                 

                                                So it seems this is all related to a module namespace issue/mismatch.

                                                 

                                                Carl

                                                • Re: Having problems with Core Data?
                                                  thecodingart_u Level 1 Level 1 (0 points)

                                                  Exact same issue here, would love to know if there is a fix.

                                                • Re: Having problems with Core Data?
                                                  miksar Level 1 Level 1 (0 points)

                                                  In Xcode 8 beta if Tools Version — Minimum in the File inspector of the .xcdatamodeld file is "Automatic (Xcode 8.0)", any changes you make to your data model will eventually disappear (for example, after opening a different file in the tab where you edited the data model and then immediately going back).

                                                   

                                                  The workaround is to select "Xcode 7.3" or older as the minimum version, make changes to your data model, build the project (optionally save and close the project, open it again and make sure everything is as it is supposed to be), and finally change the minimum version back to "Automatic (Xcode 8.0)", otherwise code generation for NSManagedObject subclasses won't work properly. Now you can create instances of the automatically generated NSManagedObject subclasses in your project.

                                                   

                                                  If changing the minimum version in Xcode doesn't help, you can try closing the project and directly editing the contents of .xcdatamodel file located inside .xcdatamodeld file whenever you need it (look for minimumToolsVersion property on the second line of the file). As always cleaning and building project should help if anything goes wrong.

                                                  • Re: Having problems with Core Data?
                                                    tiratjj Level 1 Level 1 (0 points)

                                                    Yes, I got the same problems. Could the apple team help us as soon as possible, please?

                                                    • Re: Having problems with Core Data?
                                                      Danial Level 1 Level 1 (0 points)

                                                      I had the same issue on XCode 8.2.


                                                      Finally figured out that it seems some "smart" or default setting for coredata in XCode 8.


                                                      Goto your coredata file and select the entity, change "Codegen" under class section from "Class Definition" to "Manual/None" and save.


                                                      REMEMBER: change ALL the "Codegen" setting for ALL your model.


                                                      Then, goto "Build Phases" setting of your project, add the generated model class files (include extensions) to "Compile Sources" section.


                                                      I have resolved this by these steps.


                                                      The "smart" default setting just unbelievable.

                                                        • Re: Having problems with Core Data?
                                                          gcortes Level 1 Level 1 (0 points)

                                                          I had a slightly different issue: I changed my core data model and deleted the datastore since I was importing data from a Rails API. The compiler would complain about missing files, often different files wtih each compile. The first time I changed it, I was able to fix it with option clean. The second time, that didn't work. I followed the first part of Danial's fix (i.e., change codegen) and it compiled cleanly. I'm running Xcode 8.2.1