When accessing a relationship on a CoreData object, objects of a separate, unrelated relationship are returned

Originator:openradar
Number:rdar://28798431 Date Originated:17.10.2016
Status:Open Resolved:No
Product:iOS SDK Product Version:10.0
Classification:Crash/Data Loss Reproducible:Yes
 
Summary:
(context: See example project)
Sometimes, depending on the CoreData model configuration, when accessing the `groups` relationship on the `User` entity, we get `Story` objects although the target-entity of the `groups` relationship is `Group`.

Steps to Reproduce:
1. Open the attached example project and run it. Wait for a few seconds for the initial user object and groups relationship to be set up and the first story object to be added (Log message: "Added a story").
2. Kill the app (either on the simulator or by hitting the stop-button in Xcode)
3. Run the app again. After another story was added the Assertion in `CoreDataChecker` in line 44 will be thrown. It ensures that objects return via user.groups are actually `Group`-objects which is no longer the case.

Expected Results:
The `groups` relationship should always return `Group` objects.

Actual Results:
The `groups` relationship contains `Story` objects.

Version:
iOS 10.0 (14A345)

Notes:
In the attached configuration of the data model the `User` has many ordered relationships but only one unordered relationship. In the attached configuration the `visitors` relationship is unordered.

However, it does not matter which of the relationships is unordered, as long as exactly one relationship is unordered but the others are ordered the issue exists. 
* If all relationships on User are unordered, the issue does not occur
* If two relationships on User are unordered the issue does not occur

The issue only appears on iOS 10: copying over an sqlite-database that creates a crash on iOS 10 to an iOS 9 simulator does not result in a crash (using the same data model).

There are some more comments in the code about things we tried:
* Adding stories on a background context instead of on the viewContext does not seem to provoke the issue
* Adding stories to another relationship with target-entity `Story` does not provoke the issue, only adding the to the specific `stories`-relationship provokes it, even though the relationships seem to be identical except for the name.


Note about the project: 
* We tried using both the "old way" of setting up a Core Data stack + mogenerator generated classes and the "new way" via NSPersistentContainer + Xcode generated model classes. Both have the same issue.

Configuration:
any simulator or device we run it on had the issue

Attachments:
'CoreDataFetchesWrongEntity.zip' and 'additionalNotes.txt' were successfully uploaded.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!