Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode generated CoreData files cannot be processed by Copy Bundle Resources build phase

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.

like image 592
Reinhard Männer Avatar asked Dec 08 '22 13:12

Reinhard Männer


2 Answers

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.

like image 129
Reinhard Männer Avatar answered Feb 04 '23 03:02

Reinhard Männer


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.

like image 40
EndersJeesh Avatar answered Feb 04 '23 02:02

EndersJeesh