When using predicates with multi-level relationships in SwiftData, it cannot guarantee consistent results each time

Originator:fatbobman
Number:rdar://FB13592715 Date Originated:2024/02/07
Status:Open Resolved:
Product:SwiftData Product Version:iOS 17
Classification: Reproducible:
 
When using predicates with multi-level relationships in SwiftData, it cannot guarantee consistent results each time.

Run the attached project and click on the "Using Predicate" button in the view. Observe the console output. You will get an output similar to the following:

**[PredicateTest.S_Item]**

1

Or it might be

**[PredicateTest.S_Item, PredicateTest.S_Item]**

2

After getting one type of output, click the run button in XCode to rerun the application, and after multiple switches, you might get the other type of output.

In the current code, given the environmental data, the correct output should be

**[PredicateTest.S_Item, PredicateTest.S_Item]**

2

In the project, the predicate used is:
**let** predicate = NSPredicate(format: "note == %@ OR note.parent == %@", note, note)

I have created an almost identical Core Data environment in the project, using a similar predicate:

**let** predicate = NSPredicate(format: "note == %@ OR note.parent == %@", note, note)

And each time, it yields the correct result.

PS: The SQL command generated by SwiftData's predicate is obviously more complex than Core Data's, and I wonder if this is the reason.

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!