FB17593538: Inserted child model loses its relation to parent upon save if a fetch for the parent is done using includePendingChanges false in between insert and save
Submission Date
2025-05-13
Status
Open
Area
SwiftData
Operating System Version
macOS 15.4
Type
Incorrect/Unexpected Behavior
Description
My app is dependent on using includePendingChanges false on its fetch descriptors. I noticed a bug when I do a fetch in between an insert and save. It causes any newly inserted child models to lose their relation to a parent model. This causes the database to fill up with orphaned child records.
Please open the attached MyLibrary project and select MyLibraryTests and run the example test. You will notice the test fails when the author relation of the book is checked for non-nil. The test can be run with Mac or iPhone simulator selected. If you comment out line 44 "authorsDescriptor.includePendingChanges = false" then the test will now pass. However, in my app I require to use a fetch configured like this.
By the way, I tested the same scenario with Core Data and it does not suffer from this problem hence why I have submitted this feedback.
Keywords
No response
Prerequisites
- [x] The title follows the format
FB<number>: <title> - [x] I will keep this issue updated with Apple's responses