2

We've recently decided to add a few optional Double-typed attributes (named last7, last30, and last60) to our Product CoreData entity, but this is causing an NSInternalInconsistencyException:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Property named 'sortKey' in entity 'Comment' conflicts with property inherited from parent entity 'ChatItem''

Comment is a subclass of ChatItem, and the sortKey is a String attribute declared in ChatItem, inherited by Comment. Comment has a fetch index on the sortKey property; removing this does not solve the problem.

Screenshot of Comment's Fetch Index Elements, including sortKey

As you can see in this connector view screenshot, Products are tangentially related to these two classes via the Event entity; each Product has a number of Events and each Event has a number of ChatItems. In practice, most of these ChatItems are of the subclass Comment.

Screenshot of CoreData Entity Connector View

Marking the new properties "transient" resolves the crashing issue, but does not store the data in CoreData, which is a requirement for these values.

We've noticed that this issue does not occur on clean installs, so we assume it's related to migrations in some way. Based on the accepted answer on this very similar StackOverflow question (Preventing a CoreData crash for upgrading users), we verified we have enabled lightweight migrations:

let options = [
  NSMigratePersistentStoresAutomaticallyOption: true,
  NSInferMappingModelAutomaticallyOption: true,
  NSSQLiteAnalyzeOption: true,
  NSSQLiteManualVacuumOption: true
]
store.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: self.storeURL, options: options)

We're confused because the property which is ostensibly causing the issue is not a part of an entity which we are changing; what are we missing?

Alex Mitchell
  • 390
  • 1
  • 3
  • 13
  • Is there an attribute named `sortKey` in the entity `ChatItem`? – andrewbuilder Mar 04 '20 at 22:22
  • @andrewbuilder Yes, it is defined in `ChatItem` and inherited by `Comment` – Alex Mitchell Mar 05 '20 at 00:03
  • apologies Alex, I meant to ask... does your model contain another attribute of the same name `sortKey` in the entity `Comment`. – andrewbuilder Mar 05 '20 at 00:13
  • @andrewbuilder It does not, only `ChatItem` has `sortKey` explicitly defined – Alex Mitchell Mar 05 '20 at 03:09
  • Are you aware that when using entity inheritance, Core Data framework simply creates one very large SQLite table that consists of the parent entity and all child entities? While the model may seem easier to use in the model editor and the subclassed `NSManagedObject` entities may provide convenience, the table used to persist those entities can become quite large, which depending on how you've set up your model, can lead to performance issues (albeit not the error you are currently experiencing). – andrewbuilder Mar 05 '20 at 04:24
  • Next question... in your model editor for each entity, do you set your Class Codegen to "Class Definition" or "Manual/None"? (i.e. do you manually write your NSManagedObject` subclasses or do you let Core Data framework build them for you? – andrewbuilder Mar 05 '20 at 04:28
  • I was not aware of that, @andrewbuilder, we'll definitely have to look into refactoring these data models in the future. For the codegen, it looks like we're using "Category/Extension" for all of these entities. We let CoreData generate the properties file, but have a manually maintained subclass file. – Alex Mitchell Mar 05 '20 at 15:39
  • As an aside, read this Apple Documentation titled [“Generating Code”](https://developer.apple.com/documentation/coredata/modeling_data/generating_code) – andrewbuilder Mar 05 '20 at 21:46
  • Weird one, but you havent named any properties 'new' something e.g. 'newFeatureFlag' – Sean Lintern Mar 12 '20 at 08:37
  • @SeanLintern Correct, nothing is prefixed with `new`. The only new properties are `last7`, `last30`, and `last60`. – Alex Mitchell Mar 12 '20 at 17:14
  • If you right click the .xcdatamodeld, then click "Show in Finder", you should see the embedded .xcdatamodel file. Right click that, and click "Show package contents". You should then see a file named "contents". Double click that, and you should see an xml representation of your model. I would look though that and see if it gives any clues: is the `sortKey` attribute actually defined for both Chatitem and Comment, are the `lastXX` attributes correctly defined, etc. – pbasdf Mar 15 '20 at 20:15
  • I'm seeing the same issue on iOS 14 all of a sudden. We did a minor update and did not touch our core data model at all, suddenly we're seeing similar errors and crashes. New installs are fine. I've tried everything, keeps crashing. What do we do now? – strangetimes Jan 07 '21 at 13:42
  • @strangetimes did you solve somehow this issue? – Natali May 21 '21 at 12:55
  • 1
    @Natali I left a bug report with Apple and since then got a reply - their proposed solution is multiple steps (that I haven't yet tried) and it seems to be a bug in CoreData itself; Long story short - avoid core data inheritance in future projects. – strangetimes May 25 '21 at 18:33

1 Answers1

0

I had the same issue with the exact same exception, only on iOS 14 and not on iOS 15.

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Property named 'myproperty' in entity '' conflicts with property inherited from parent entity '''

I was able to solve the problem by removing indices on the source model version for the migration.

Note: The indices were not directly referencing the key 'myproperty' in question, but were indices on keys that were renamed into 'myproperty'. This clearly looks like an iOS 14 bug.

Kamchatka
  • 3,597
  • 4
  • 38
  • 69
  • I had the same issue and also removed the indices from the current model version and this seemed to work in the simulator. However I still see a lot of these crashes happening. Could you elaborate a bit what your exact steps were? I'm a bit reluctant to change previous model versions, doesn't seem right... – weak Feb 17 '22 at 14:48