Post

Replies

Boosts

Views

Activity

Reply to ModelContext.model(for:) returns deleted objects
I'm writing some tests to confirm the behavior of my app, you are actually unit testing SwiftData and not your app. As for the problem, I am not sure it's a bug and that they deliberately keep the object in the ModelContext for a short while to avoid problems for the app using it. The following test would pass though #expect(result.isDeleted)
Topic: App & System Services SubTopic: iCloud Tags:
May ’25
Reply to SwiftData duplicates values inside array on insert()
I didn't notice Feature was a many-to-many relationship, I thought the array was owned by the Car/CarData object, my bad. There is really no point in making your models conform to Identifiable and have a custom id property since they already conform to this protocol (unless you have some specific reason to use your own identifier value). I have no idea if this could help with the duplicate Feature objects issue but maybe it could be worth trying by removing the id property and use the default one instead.
Topic: App & System Services SubTopic: iCloud Tags:
Mar ’25
Reply to SwiftData superclass prevents usage of ID
Why don’t you have a proper relationship for assetSelection and use the type [Asset] instead?
Replies
Boosts
Views
Activity
Jun ’25
Reply to Fatal error: Duplicate keys of type 'AnyHashable2' were found in a Dictionary.
When a model object is saved its identifier is changed from a temporary one to a permanent one (so I assume the hash values also changes) so this is most likely what cases an issue here with the picker. One way to work around this is to call save() directly after the inserts.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’25
Reply to Are these @model classes correct for swiftdata with cloudkit?
You don’t need to use the @Attribute macro for your properties at all unless you want to give them some custom behavior.
Topic: App & System Services SubTopic: iCloud Tags:
Replies
Boosts
Views
Activity
Jun ’25
Reply to Migrating a swiftData project to CloudKit to implement iCloudSync.
The same. Just make sure you always initialize the to-many properties to an empty array as you have done in your example
Topic: App & System Services SubTopic: iCloud Tags:
Replies
Boosts
Views
Activity
Jun ’25
Reply to No persistent stores error in SwiftData
Initializing the ModelContainer in an onAppear of a view is too late, you need to do it directly in MyApp where yo declare the property itself.
Topic: App & System Services SubTopic: iCloud Tags:
Replies
Boosts
Views
Activity
May ’25
Reply to CoreData error: SQLCore dispatchRequest: no such table: ZAPPSETTINGS. I/O error opening
You could enable Core Data debug logging (also used by SwiftData) to see if that gives you any important information, see https://www.hackingwithswift.com/quick-start/swiftdata/using-launch-arguments-to-debug-swiftdata-and-core-data
Topic: Community SubTopic: Apple Developers Tags:
Replies
Boosts
Views
Activity
May ’25
Reply to Migrating a swiftData project to CloudKit to implement iCloudSync.
You could make the model properties private and rename them and then add computed properties with the old name that you can access from outside the class. var someString: String could be replaced with private var someStringStored: String? var someString: String { get { someStringStored ?? "" } set { someStringStored = newValue } }
Topic: App & System Services SubTopic: iCloud Tags:
Replies
Boosts
Views
Activity
May ’25
Reply to ModelContext.model(for:) returns deleted objects
I'm writing some tests to confirm the behavior of my app, you are actually unit testing SwiftData and not your app. As for the problem, I am not sure it's a bug and that they deliberately keep the object in the ModelContext for a short while to avoid problems for the app using it. The following test would pass though #expect(result.isDeleted)
Topic: App & System Services SubTopic: iCloud Tags:
Replies
Boosts
Views
Activity
May ’25
Reply to SwiftData/ModelSnapshot.swift:46: Fatal error: A ModelSnapshot must be initialized with a known-keys dictionary
I tried to reproduce on a iOS 18.4 large but failed, which isn't that surprising given how little information you have provided. Could you add enough context and code so we can reproduce this issue?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’25
Reply to Mac App Crashing with Illegal Instructions
Maybe the underscore in the property name is causing an issue, could you try with a different name? And shouldn't that property be private so you can only access the relationship in one way?
Topic: App & System Services SubTopic: iCloud Tags:
Replies
Boosts
Views
Activity
Apr ’25
Reply to SwiftData crashes on fetchHistory
A crash and a thrown error are two completely different things and you can not replace a crash with a thrown error with the exception of if you are deliberately crashing by calling fatalError(_) but I very much doubt that is what is happening here.
Topic: App & System Services SubTopic: iCloud Tags:
Replies
Boosts
Views
Activity
Apr ’25
Reply to Swift Data initiate
You are going to need to include relevant code and/or a more detailed description of how this happens if you want us to be able to help you
Topic: App & System Services SubTopic: iCloud Tags:
Replies
Boosts
Views
Activity
Apr ’25
Reply to SwiftData "Auto Inserts" array into ModelContext
SwiftData does it automatically for you and it must do it or the autosave functionality wouldn't work. If you would only save one side of a relationship then when the app is restarted the other side would be nil or it crashes if the property is non-optional.
Topic: App & System Services SubTopic: iCloud Tags:
Replies
Boosts
Views
Activity
Apr ’25
Reply to UserDefaults to SwifData Migration
if you’re asking if there’s an automatic way then no there isn’t but doing this should be pretty straightforward, check if the move has already been done when the app starts and if not load the data from UserDefaults and store it using your SwiftData models.
Topic: App & System Services SubTopic: iCloud Tags:
Replies
Boosts
Views
Activity
Mar ’25
Reply to SwiftData duplicates values inside array on insert()
I didn't notice Feature was a many-to-many relationship, I thought the array was owned by the Car/CarData object, my bad. There is really no point in making your models conform to Identifiable and have a custom id property since they already conform to this protocol (unless you have some specific reason to use your own identifier value). I have no idea if this could help with the duplicate Feature objects issue but maybe it could be worth trying by removing the id property and use the default one instead.
Topic: App & System Services SubTopic: iCloud Tags:
Replies
Boosts
Views
Activity
Mar ’25