JUST ENDED
|

SwiftData Q&A

Connect with Apple engineers in the SwiftData Q&A on the Apple Developer Forums.

Post

Replies

Boosts

Views

Activity

Fetch data in a time range, plus extend one more entry
I’m using Swift Charts in my app to display data in a visual way. For that (as well as some sum calculations) I’d like to create a fetch request that searches for all the data in a given time range plus one older and newer entry. Building a predicate that searches for a time range is easy, but how can I tell the fetch request that I would also like to have the first entry that comes before that time range and the first that comes after? I don’t know when these entries appear, so I can’t extend the time range. Example Let’s say I have the following data (timestamp – value): Feb 1, 2024 – 20 Oct 1, 2024 – 30 Jan 1, 2025 – 40 Sep 1, 2025 – 50 May 1, 2026 – 60 Oct 1, 2026 – 70 Now I want to search for the range Jan to Dec 2025. How can I retrieve the two entries that are in the range (No. 3 & 4) as well as the first entry before (No. 2) and the first after that (No. 5)? Without knowing the timestamp of No. 2 and 5. I’d appreciate some best practices or tips how to handle this special case. 🙏
1
0
60
15h
SwiftData predicate filtered by enum case
I have several Swift Data types with a property of type enum. Whenever I've tried to write a predicate returning data objects only of a certain enum case, the compiler throws an error from the macro at build time. (which I don't have handy, sorry...). Is this supported? And if so, how would you write the predicate? @Model public final class AlbumList { // ... public var listType: AlbumListType // ... } public enum AlbumListType: String, CaseIterable, Codable { case listener case dj }
2
0
54
15h
Migration Hash Mismatch
I'm migrating an existing Core Data app to SwiftData and encountering a persistent schema hash mismatch that prevents the store from loading. The Core Data store has been in production for years with version 4 of the model. I have two models, lets call the Crew and Astronaut Create is a parent in the to-many relationship to Astronaut, the child. When I try the migration, the Crew model‘s hash matches, but the Astronaut model’s mismatches. I also tried changing relationship parameters; deleteRule, maximumModelCount, inverse, originalName, and hashModifier. hashModifier changes the hash, but to a third value, so it does not appear to be a way to match the existing Core Data hash. So, is there a supported way for SwiftData to generate an optional to-one relationship that is Core Data hash-compatible with optional=YES minCount=1 maxCount=1? Or is a migration/rehash/copy step required before SwiftData can adopt this V4 store in place?
3
0
47
15h
SwiftData + public CloudKitDatabase?
Greetings, Thank you for SwiftData + new features. I am looking forward to the SwiftData Group Lab on Friday. How does the performance of SwiftData + CloudKit private database compare to CoreData + NSPersistenceCloudKitContainer? Also, will SwiftData + CloudKit support the CK public database, in the future? Thank you!
2
2
65
15h
Dynamic Compound Predicates
This is relating to the question I have for the App Intents framework (see my question). I know that SwiftData started to support compound predicates with macOS 14.4/iOS 17.4 or later. But from what I understand they are not dynamic and are validated at compile time. Is there a way to update/construct predicates while the app is running? For example to create a search tab that allows searching and filtering for my items in the app.
1
0
52
15h
Sectioned fetching: beyond String keyPaths?
Hello, world! First and foremost, thank you for all the enhancements made to SwiftData and kudos to the team! In Thomas’ video demonstrating “What’s new in SwiftData”, I notice the keyPath (trip.destination) used to create sections in the list is a String. I can’t help but wonder if a keyPath of another data type could be used (like trip.startDate), were one to want to create sections based on whether a trip is past or upcoming for example? Thank you in advance for your much appreciated input! Zoé
1
0
73
16h
Fetch data in a time range, plus extend one more entry
I’m using Swift Charts in my app to display data in a visual way. For that (as well as some sum calculations) I’d like to create a fetch request that searches for all the data in a given time range plus one older and newer entry. Building a predicate that searches for a time range is easy, but how can I tell the fetch request that I would also like to have the first entry that comes before that time range and the first that comes after? I don’t know when these entries appear, so I can’t extend the time range. Example Let’s say I have the following data (timestamp – value): Feb 1, 2024 – 20 Oct 1, 2024 – 30 Jan 1, 2025 – 40 Sep 1, 2025 – 50 May 1, 2026 – 60 Oct 1, 2026 – 70 Now I want to search for the range Jan to Dec 2025. How can I retrieve the two entries that are in the range (No. 3 & 4) as well as the first entry before (No. 2) and the first after that (No. 5)? Without knowing the timestamp of No. 2 and 5. I’d appreciate some best practices or tips how to handle this special case. 🙏
Replies
1
Boosts
0
Views
60
Activity
15h
SwiftData predicate filtered by enum case
I have several Swift Data types with a property of type enum. Whenever I've tried to write a predicate returning data objects only of a certain enum case, the compiler throws an error from the macro at build time. (which I don't have handy, sorry...). Is this supported? And if so, how would you write the predicate? @Model public final class AlbumList { // ... public var listType: AlbumListType // ... } public enum AlbumListType: String, CaseIterable, Codable { case listener case dj }
Replies
2
Boosts
0
Views
54
Activity
15h
Migration Hash Mismatch
I'm migrating an existing Core Data app to SwiftData and encountering a persistent schema hash mismatch that prevents the store from loading. The Core Data store has been in production for years with version 4 of the model. I have two models, lets call the Crew and Astronaut Create is a parent in the to-many relationship to Astronaut, the child. When I try the migration, the Crew model‘s hash matches, but the Astronaut model’s mismatches. I also tried changing relationship parameters; deleteRule, maximumModelCount, inverse, originalName, and hashModifier. hashModifier changes the hash, but to a third value, so it does not appear to be a way to match the existing Core Data hash. So, is there a supported way for SwiftData to generate an optional to-one relationship that is Core Data hash-compatible with optional=YES minCount=1 maxCount=1? Or is a migration/rehash/copy step required before SwiftData can adopt this V4 store in place?
Replies
3
Boosts
0
Views
47
Activity
15h
SwiftData + public CloudKitDatabase?
Greetings, Thank you for SwiftData + new features. I am looking forward to the SwiftData Group Lab on Friday. How does the performance of SwiftData + CloudKit private database compare to CoreData + NSPersistenceCloudKitContainer? Also, will SwiftData + CloudKit support the CK public database, in the future? Thank you!
Replies
2
Boosts
2
Views
65
Activity
15h
Dynamic Compound Predicates
This is relating to the question I have for the App Intents framework (see my question). I know that SwiftData started to support compound predicates with macOS 14.4/iOS 17.4 or later. But from what I understand they are not dynamic and are validated at compile time. Is there a way to update/construct predicates while the app is running? For example to create a search tab that allows searching and filtering for my items in the app.
Replies
1
Boosts
0
Views
52
Activity
15h
Sectioned fetching: beyond String keyPaths?
Hello, world! First and foremost, thank you for all the enhancements made to SwiftData and kudos to the team! In Thomas’ video demonstrating “What’s new in SwiftData”, I notice the keyPath (trip.destination) used to create sections in the list is a String. I can’t help but wonder if a keyPath of another data type could be used (like trip.startDate), were one to want to create sections based on whether a trip is past or upcoming for example? Thank you in advance for your much appreciated input! Zoé
Replies
1
Boosts
0
Views
73
Activity
16h
Aggregate functions in SwiftData
Hi, does SwiftData supports aggregate functions through NSExpression for operations like SUM, AVG, MIN, and MAX?
Replies
1
Boosts
0
Views
62
Activity
16h