7

I added CoreData to my app MY_APP:

  • I defined the data model by creating a xcdatamodeld file containing a single entity XXX with a few attributes.
  • Using Xcode/Editor/Create NSManagedSubclass, Xcode created 2 files, XXX+CoreDataClass.swift and XXX+CoreDataProperties.swift.
  • I wrote a little code to test storage and fetch back from core data, and everything works fine.

The problem:
At the beginning of the build phase, I get 3 warnings:

warning: The Swift file "/Users/reiner/Library/Developer/Xcode/DerivedData/  
MY_APP/Build/Intermediates.noindex/MY_APP.build/Debug-iphonesimulator/  
MY_APP.build/DerivedSources/CoreDataGenerated/MY_APP/XXX+CoreDataClass.swift"  
cannot be processed by a Copy Bundle Resources build phase (in target ‚MY_APP‘)  

warning: The Swift file "/Users/reiner/Library/Developer/Xcode/DerivedData/  
MY_APP/Build/Intermediates.noindex/MY_APP.build/Debug-iphonesimulator/  
MY_APP.build/DerivedSources/CoreDataGenerated/MY_APP/XXX+CoreDataProperties.swift"  
cannot be processed by a Copy Bundle Resources build phase (in target 'MY_APP')  

warning: The Swift file "/Users/reiner/Library/Developer/Xcode/DerivedData/  
MY_APP/Build/Intermediates.noindex/MY_APP.build/Debug-iphonesimulator/  
MY_APP.build/DerivedSources/CoreDataGenerated/MY_APP/MY_APP+CoreDataModel.swift"  
cannot be processed by a Copy Bundle Resources build phase (in target 'MY_APP')  

These 3 files are not listed under MY_APP target/Build Phases/Copy Bundle Resources.

My questions:
Is anything wrong with my build setup, i.e. what is the reason for these warnings, and how can I avoid it?

Remark: This question relates to a different framework (core data), but is similar to this one, which does not have an answer yet.

EDIT:
My project has 2 targets, for iOS and for watchOS. Until now, core data was used only on iOS.
I tried now to enable it also for watchOS, but I got an error, because the .xcdatamodeld was not yet in Build Phases / Copy Bundle Resources.
As soon as I added it there, core data was executed correctly on the watch.

BUT: I got the same 3 warnings mentioned above, this time additionally for the watch extension target (altogether 6 warnings).
Maybe this a useful hint.

Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116

3 Answers3

15

EDIT:

I contacted Apple, and they provided a solution:

When an Xcode project „xxx“ is created with the coreData option on, a core data model file „xxx.xcdatamodeld“ is created and added to the target Build Phases Compile Sources.

Say, one adds there an entity „Entity“ with an attribute „attribute“.

If one selects in the Xcode project navigator this model file and opens the file inspector, there is an entry „Code Generation“ which is set to Swift by default. This creates the 2 files Entity+CoreDataClass.swift and Entity+CoreDataProperties.swift automatically, but they are not shown in the project navigator.
Building the project succeeds, and one can use a property Entity.attribute as usual in the code.

However:
If one selects the xcdatamodeld file in the Xcode navigator, the Xcode Editor menu has an entry „Create NSManagedObject Subclass…“. If one selects this entry and there the xxx data model, the 2 files Entity+CoreDataClass.swift and Entity+CoreDataProperties.swift are created again and shown in the project navigator, and added by default to the target.

This means that these files are added twice, thus the warnings.

So the solution is not to use this editor command, and I don’t know what it is for…

EDIT 2:

My fault; I was looking at the wrong place:

  • Open the xcdatamodeld in the project navigator.
  • In the pane right of it, select an entity.
  • At the top right, open the inspector pane.
  • At the top right of it, select the data model inspector.
  • There is an entry „Codegen“ where one can select Manual/None.

If this option is selected, no code is automatically generated from xcdatamodeld, i.e., one can manually (by using the editor command) create NSManagedObject subclasses that can be added to the target Compile Sources section, as required.

Previous answer:

There are apparently 2 ways to use CoreData, either 1) by using only the PROJECT.xcdatamodeld file, which is then added to Compile Sources Build Phase, or 2) by creating a NSManagedObject subclass using Xcode’s Editor/Create NSManagedObject Subclass command.

If 1) is used, everything works fine, but one has no property access to the entity used.

If 2) is used, Xcode creates the 2 files ENTITY+CoreDataClass.swift and ENTITY+CoreDataProperties.swift. These 2 files are added to the Compile Sources Build Phase, but PROJECT.xcdatamodeld must not. If one does anyway, one gets the build error „unexpected duplicate task“. But if one does not, the project builds without errors and warnings.
However, when run, the instruction

let entity = NSEntityDescription.entity(forEntityName: "MyEntity", in: managedContext)! 

fails, because it does not find the data model.
A workaround is to add PROJECT.xcdatamodeld to target / Build Phases / Copy Bundle Resources. Then the code executes fine, but one will get the warnings that I described in my question.

Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116
3

For me what caused the issue was having the .xcdatamodeld file in the Copy Bundle Resources step within Build Phases for the target specified in the warning: in your case, MY_APP. I removed that file from the Copy Bundle Resources step and all the warnings went away.

EndersJeesh
  • 427
  • 1
  • 4
  • 20
  • 1
    As I mentioned in my own answer, removing `.xcdatamodeld` from the `Copy Bundle Resources` does indeed remove the warnings. However, when I do so, `NSPersistentContainer(name:)` returns now (in my case) `nil`, so core data is not usable. Does it work in your case? – Reinhard Männer Jan 06 '19 at 07:59
  • Ah sorry I missed that detail. I do not have a watch build so I haven't had to deal with that issue. This is for a pod though, so I could see myself running into a similar issue when this code is used as a pod. Thanks for clarifying that; I'll definitely need to keep an eye out on that, and I'm sorry I can't be of more help at this time. – EndersJeesh Jan 07 '19 at 18:10
0

An approach to dealing with an Xcode which seems to be getting more and more buggy with each release in recent years:

  • Quit and relaunch Xcode.
  • If that does not work, do a Show Package Contents on the .xcodeproj package and open the .pbxproj file in a text editor. Search the file for occurrences of XXX+CoreDataClass. The search the file for occurrences of some other .swift file which does not create this warning. Compare the two search results. It may be necessary to manually edit the .pbxproj file.
Jerry Krinock
  • 4,860
  • 33
  • 39
  • Quit & relaunch of Xcode did not help. And I hesitate to edit the `.pbxproj` file for 2 reasons: 1) I do not understand what I would do, and 2) I did find the first 2 files that I mentioned in my question in `.pbxproj`, but not the 3rd one. So I guess the problem could not be solved completely by your suggestion. Thanks anyway! – Reinhard Männer Dec 13 '18 at 17:58
  • I had to edit a `.pbxproj` file for a similar reason a couple months ago. It is not difficult. Each item (file, target, build phase, etc.) is referenced by a 24-hex-digit identifier. The file is essentially a bunch of lists which look similar to JSON arrays. Of course, you only edit the file when Xcode is quit. If you screw anything up, it won't open – try again with your backup. As far as the 3rd file not in the `.pbxproj` file but still showing up in the user interface, I don't think that is possible. Anyhow, if you are successful without editing `.pbxproj`, please post your answer! – Jerry Krinock Dec 13 '18 at 23:50
  • I searched in `project.pbxproj` for anything containing `+CodeData`, since this is part of all 3 files mentioned in the warnings: - The PBXBuildFile section contains `MY_APP+CoreDataClass.swift` and `MY_APP+CoreDataProperties.swift`, but both entries identically twice. - It does not contain `MY_APP+CoreDataModel.swift`. Since I found it strange that the same entries are contained twice, I deleted one copy each. This resulted in build errors in addition to the warnings. I then searched for `MY_APP+CoreDataModel.swift`, and found it in the Derived Data, like the other 2 files. – Reinhard Männer Dec 14 '18 at 15:20
  • Copy and search for the 16-hex-digit which appears to be associated with a "good" file. Repeat for one of the "bad" files. Compare the two search results. – Jerry Krinock Dec 14 '18 at 17:31