Search results for

“SwiftData inheritance relationship”

4,980 results found

Post

Replies

Boosts

Views

Activity

SwiftData crash on adding sort argument to Query
Experiencing a crash that is only reproducible on TestFlight or AppStore version of the app, note this does not happen when running from Xcode. I've isolated the problem to sort argument being added to @Query that fetches a model that sorts based on inherited property. To reproduce: @Model class SuperModel { var createdAt: Date = .now } @available(macOS 26.0, *) @Model class SubModel: SuperModel { } @Query(sort: SubModel.createdAt, animation: .default) private var models: [SubModel]
1
0
151
Jan ’26
Reply to Sharing SwiftData between two apps
Assuming that your second app is under the same team, you can create an app group container for the two apps to share, and then have your first app move its data store to the container, your second app should then be able to access the store. To create an app group container, see Configuring app groups. To locate a SwiftData store to an app group container, see Share the same store file and the associate sample project. Best, —— Ziqiao Chen  Worldwide Developer Relations.
Jan ’26
Extreme increase in app storage size after enabling CloudKit
I have a SwiftData flashcard app which I am syncing with CloudKit using NSPersistentCloudKitContainer. While syncing itself is working perfectly, I have noticed a dramatic increase in the app size after enabling sync. Specifically, without CloudKit, 15k flashcards results in the default.store file being about 4.5 MB. With CloudKit, default.store is about 67 MB. I have inspected the store and found that most of this increase is due to the ANSCKRECORDMETADATA table. My question is, does implementing CloudKit normally cause this magnitude of increase in storage? If it doesn’t, is there something in my model, schema, implementation, etc. that could be causing it? Below are two other posts describing a similar issue, but neither with a solution. I replied to the first one about a month ago. I then submitted this to Developer Technical Support, but was asked to post my question in the forums, so here it is. Strange behavior with 100k+ records in NSPersistentCloudKitContainer Huge increase in sqlite file si
2
0
206
Jan ’26
Reply to Extreme increase in app storage size after enabling CloudKit
When you use SwiftData + CloudKit, under the hood, the SwiftData framework converts SwiftData models to Core Data managed objects, and use NSPersistentCloudKitContainer to synchronize the objects with CloudKit. NSPersistentCloudKitContainer mirrors each Core Data managed object to (at least) a CloudKit record (CKRecord), as described in Reading CloudKit Records for Core Data. To manage the internal state of the synchronization, it adds a few tables to the store, and the ANSCKRECORDMETADATA table is one of them, which is used to store the metadata of the CloudKit records. The metadata includes the system fields and encoded data of every CloudKit record, so yes, you can see that the size of table gets quite large. The ANSCKRECORDMETADATA table is an internal data structure that NSPersistentCloudKitContainer creates and consumes. You can review the documentation mentioned above to make sure that your models are translated to CloudKit schema correctly, but other than that, I don't see a
Jan ’26
Providing pre-filled sample data for reviewer experience in Swift Student Challenge: Is it recommended?
Hi everyone, I am currently developing an app for my Swift Student Challenge submission. One of the key features of my app is visualizing user progress over time using SwiftData and Swift Charts. I have a concern regarding the first-time experience for the reviewer. Since the app relies on accumulated data to display meaningful trends, the dashboard/charts will appear empty on the very first launch, which might not fully showcase the visualization logic I’ve implemented. To ensure the reviewer can immediately grasp the app's potential within their limited review window, I am considering generating pre-populated sample data (mock data) only on the initial launch. Does Apple generally recommend including sample data in a challenge submission to better demonstrate UI/UX and data visualization capabilities? Or is it strictly preferred to present a fresh empty state, as a real first-time user would see it? I want to make sure I am following the best practices for the challenge while highlighting my techni
1
0
442
Jan ’26
SwiftData CloudKit sync broken on iOS 26
Hi everyone, I’m running into a breaking issue with SwiftData automatic CloudKit syncing on iOS 26, and I'm trying to determine if this is a known regression or a new configuration requirement I missed. The Setup: My setup is extremely standard; I am using the default configuration exactly as described in Apple's documentation here: https://developer.apple.com/documentation/swiftdata/syncing-model-data-across-a-persons-devices The schema is very simple: A single @Model class. No relationships. The Issue: Prior to iOS 26, this exact app was successfully syncing data between devices and to iCloud without issues. Immediately after the iOS 26 update, syncing stopped completely. I haven't changed any code, but when I check the CloudKit Console, I am seeing some BAD_REQUEST errors during sync attempts. Since I am using the default SwiftData sync (and not manual CKRecord handling), I’m not sure how my client code could be triggering a bad request unless the schema requirements hav
1
0
295
Jan ’26
SwiftData with CloudKit Sync Issue
I am using SwiftData with CloudKit to synchronize data across multiple devices, and I have encountered an issue: occasionally, abnormal sync behavior occurs between two devices (it does not happen 100% of the time—only some users have reported this problem). It seems as if synchronization between the two devices completely stops; no matter what operations are performed on one end, the other end shows no response. After investigating, I suspect the issue might be caused by both devices simultaneously modifying the same field, which could lead to CloudKit's logic being unable to handle such conflicts and causing the sync to stall. Are there any methods to avoid or resolve this situation? Of course, I’m not entirely sure if this is the root cause. Has anyone encountered a similar issue?
2
0
296
Jan ’26
NSPersistentCloudKitContainer data loss edge case
Hi, I was testing the new iOS 18 behavior where NSPersistentCloudKitContainer wipes the local Core Data store if the user logs out of iCloud, for privacy purposes. I ran the tests both with a Core Data + CloudKit app, and a simple one using SwiftData with CloudKit enabled. Results were identical in either case. In my testing, most of the time, the feature worked as expected. When I disabled iCloud for my app, the data was wiped (consistent with say the Notes app, except if you disable iCloud it warns you that it'll remove those notes). When I re-enabled iCloud, the data appeared. (all done through the Settings app) However, in scenarios when NSPersistentCloudKitContainer cannot immediately sync -- say due to rate throttling -- and one disables iCloud in Settings, this wipes the local data store and ultimately results in data loss. This occurs even if the changes to the managed objects are saved (to the local store) -- it's simply they aren't synced in time. It can be a little hard to reproduce the is
3
0
375
Jan ’26
Reply to unifiedContacts identifier vs contactRelations identifier
Here's the test code where I was attempting to find the relationship between records. // ContentView.swift // TestContactsProject // import SwiftUI import Contacts import OSLog extension Logger { private static var subsystem = Bundle.main.bundleIdentifier ?? com.myelin.TestContactsProject static let logic = Logger(subsystem: subsystem, category: logic) static let analitics = Logger(subsystem: subsystem, category: analitics) } struct ContentView: View { var body: some View { Text(Hello, World!) .padding() .onAppear { Task.init { await fetchAllContacts() //await fetchSpecificContacts() } } } func fetchSpecificContacts(keyStrings: [String]) { // Run this in the background async // Get access to the Contacts Store let store = CNContactStore() // Keys to fetch let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey, CNContactIdentifierKey, CNContactRelationsKey] as [CNKeyDescriptor] // Search criteria let predicate = CNContact.predicateForContacts(withIdentifiers: keyStrings) d
Topic: App & System Services SubTopic: General Tags:
Dec ’25
Reply to Bug apple Health
// // OnboardingContainerView.swift // Worko // // Created by Rodrigo Soares on 20/12/25. // import SwiftUI import SwiftData /// Container que gerencia todo o fluxo de navegação do onboarding struct OnboardingContainerView: View { // MARK: - Properties @Environment(AppStateManager.self) private var appState @Environment(.modelContext) private var modelContext @State private var viewModel = OnboardingViewModel() @State private var isHealthKitReady = false // MARK: - Body var body: some View { ZStack { WorkoColors.background.ignoresSafeArea() // Content com transições ZStack { currentStepView .transition(.creativeSlide(direction: viewModel.navigationDirection)) } .animation(.spring(response: 0.6, dampingFraction: 0.8, blendDuration: 0), value: viewModel.currentStep) // Controles fixos (header + botão) if viewModel.currentStep != .completion { controlsOverlay } } .task { await viewModel.requestHealthKitAuthorization() isHealthKitReady = true } } // MARK: - Current Step View @ViewBuilder private var curr
Dec ’25
SwiftData .autosaveEnabled / rollback() trouble
Hello, In my iOS/SwiftUI/SwiftData app, I want the user to be able to hit [Cancel] from editing in a detail screen and return to the previous screen without changes being saved. I believed that setting autosaveEnabled to false and/or calling .rollback would prevent changes from being saved, unless/until I call .save() when the user clicks [Save], but this does not seem to be correct. I set modelContext.autosaveEnabled = false and I call modelContext.rollback() when the user hits [Cancel], but any changes they made are not rolled back, but saved even if I don’t call save(). I have tried setting autosaveEnabled to false when I create the ModelContainer on a @MainActor function when the App starts, and in the detail/edit screen’s .onAppear(). I can see that .rollback is being called when the [Cancel] button is tapped. In all cases, any changes the user made before hitting [Cancel] are saved. The Developer Documentation on autosaveEnabled includes this: “The default value is false. SwiftData aut
4
0
263
Dec ’25
Reply to NSWindowController subclass in Swift
You need to add override init(window: NSWindow?) { super.init(window: window) } in your subclass. See https://docs.swift.org/swift-book/documentation/the-swift-programming-language/initialization/ which say: Rule 1 If your subclass doesn’t define any designated initializers, it automatically inherits all of its superclass designated initializers. Rule 2 If your subclass provides an implementation of all of its superclass designated initializers — either by inheriting them as per rule 1, or by providing a custom implementation as part of its definition — then it automatically inherits all of the superclass convenience initializers. The (unstated) corollary here is that if you only override some of the superclass's designated initializers, you inherit none of them. in your case, you'd overridden init(coder:) not init(window:), so the compiler didn't 'see' an init(window:) at all. I'm not smart enough to figure this out on my own, I asked an LLM and then asked it to show me th
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’25
SwiftData crash on adding sort argument to Query
Experiencing a crash that is only reproducible on TestFlight or AppStore version of the app, note this does not happen when running from Xcode. I've isolated the problem to sort argument being added to @Query that fetches a model that sorts based on inherited property. To reproduce: @Model class SuperModel { var createdAt: Date = .now } @available(macOS 26.0, *) @Model class SubModel: SuperModel { } @Query(sort: SubModel.createdAt, animation: .default) private var models: [SubModel]
Replies
1
Boosts
0
Views
151
Activity
Jan ’26
Reply to SwiftData crash on adding sort argument to Query
A similar issue was discussed in this thread. Your case has another layer of complexity - inheritance. Would you mind to have a check if your issue still happens without using inheritance? Also, would you mind to share the OS and SDK version you tested with? Thanks. Best, —— Ziqiao Chen  Worldwide Developer Relations.
Replies
Boosts
Views
Activity
Jan ’26
Reply to Sharing SwiftData between two apps
Assuming that your second app is under the same team, you can create an app group container for the two apps to share, and then have your first app move its data store to the container, your second app should then be able to access the store. To create an app group container, see Configuring app groups. To locate a SwiftData store to an app group container, see Share the same store file and the associate sample project. Best, —— Ziqiao Chen  Worldwide Developer Relations.
Replies
Boosts
Views
Activity
Jan ’26
Sharing SwiftData between two apps
This is probably super simple answer that I missed, but: I have an app that has a database; I'd like to create a second app (actually a CLI tool), and access the same database. Is that possible? And, if so, how? 😄
Replies
1
Boosts
0
Views
388
Activity
Jan ’26
Extreme increase in app storage size after enabling CloudKit
I have a SwiftData flashcard app which I am syncing with CloudKit using NSPersistentCloudKitContainer. While syncing itself is working perfectly, I have noticed a dramatic increase in the app size after enabling sync. Specifically, without CloudKit, 15k flashcards results in the default.store file being about 4.5 MB. With CloudKit, default.store is about 67 MB. I have inspected the store and found that most of this increase is due to the ANSCKRECORDMETADATA table. My question is, does implementing CloudKit normally cause this magnitude of increase in storage? If it doesn’t, is there something in my model, schema, implementation, etc. that could be causing it? Below are two other posts describing a similar issue, but neither with a solution. I replied to the first one about a month ago. I then submitted this to Developer Technical Support, but was asked to post my question in the forums, so here it is. Strange behavior with 100k+ records in NSPersistentCloudKitContainer Huge increase in sqlite file si
Replies
2
Boosts
0
Views
206
Activity
Jan ’26
Reply to Extreme increase in app storage size after enabling CloudKit
When you use SwiftData + CloudKit, under the hood, the SwiftData framework converts SwiftData models to Core Data managed objects, and use NSPersistentCloudKitContainer to synchronize the objects with CloudKit. NSPersistentCloudKitContainer mirrors each Core Data managed object to (at least) a CloudKit record (CKRecord), as described in Reading CloudKit Records for Core Data. To manage the internal state of the synchronization, it adds a few tables to the store, and the ANSCKRECORDMETADATA table is one of them, which is used to store the metadata of the CloudKit records. The metadata includes the system fields and encoded data of every CloudKit record, so yes, you can see that the size of table gets quite large. The ANSCKRECORDMETADATA table is an internal data structure that NSPersistentCloudKitContainer creates and consumes. You can review the documentation mentioned above to make sure that your models are translated to CloudKit schema correctly, but other than that, I don't see a
Replies
Boosts
Views
Activity
Jan ’26
Providing pre-filled sample data for reviewer experience in Swift Student Challenge: Is it recommended?
Hi everyone, I am currently developing an app for my Swift Student Challenge submission. One of the key features of my app is visualizing user progress over time using SwiftData and Swift Charts. I have a concern regarding the first-time experience for the reviewer. Since the app relies on accumulated data to display meaningful trends, the dashboard/charts will appear empty on the very first launch, which might not fully showcase the visualization logic I’ve implemented. To ensure the reviewer can immediately grasp the app's potential within their limited review window, I am considering generating pre-populated sample data (mock data) only on the initial launch. Does Apple generally recommend including sample data in a challenge submission to better demonstrate UI/UX and data visualization capabilities? Or is it strictly preferred to present a fresh empty state, as a real first-time user would see it? I want to make sure I am following the best practices for the challenge while highlighting my techni
Replies
1
Boosts
0
Views
442
Activity
Jan ’26
SwiftData CloudKit sync broken on iOS 26
Hi everyone, I’m running into a breaking issue with SwiftData automatic CloudKit syncing on iOS 26, and I'm trying to determine if this is a known regression or a new configuration requirement I missed. The Setup: My setup is extremely standard; I am using the default configuration exactly as described in Apple's documentation here: https://developer.apple.com/documentation/swiftdata/syncing-model-data-across-a-persons-devices The schema is very simple: A single @Model class. No relationships. The Issue: Prior to iOS 26, this exact app was successfully syncing data between devices and to iCloud without issues. Immediately after the iOS 26 update, syncing stopped completely. I haven't changed any code, but when I check the CloudKit Console, I am seeing some BAD_REQUEST errors during sync attempts. Since I am using the default SwiftData sync (and not manual CKRecord handling), I’m not sure how my client code could be triggering a bad request unless the schema requirements hav
Replies
1
Boosts
0
Views
295
Activity
Jan ’26
SwiftData with CloudKit Sync Issue
I am using SwiftData with CloudKit to synchronize data across multiple devices, and I have encountered an issue: occasionally, abnormal sync behavior occurs between two devices (it does not happen 100% of the time—only some users have reported this problem). It seems as if synchronization between the two devices completely stops; no matter what operations are performed on one end, the other end shows no response. After investigating, I suspect the issue might be caused by both devices simultaneously modifying the same field, which could lead to CloudKit's logic being unable to handle such conflicts and causing the sync to stall. Are there any methods to avoid or resolve this situation? Of course, I’m not entirely sure if this is the root cause. Has anyone encountered a similar issue?
Replies
2
Boosts
0
Views
296
Activity
Jan ’26
Reply to SwiftData with CloudKit Sync Issue
I have had the same issue with SwiftData and CloudKit after iOS 26 release. My setup is super minimal and using the default configuration so no clue what could have gone wrong on my side.
Replies
Boosts
Views
Activity
Jan ’26
NSPersistentCloudKitContainer data loss edge case
Hi, I was testing the new iOS 18 behavior where NSPersistentCloudKitContainer wipes the local Core Data store if the user logs out of iCloud, for privacy purposes. I ran the tests both with a Core Data + CloudKit app, and a simple one using SwiftData with CloudKit enabled. Results were identical in either case. In my testing, most of the time, the feature worked as expected. When I disabled iCloud for my app, the data was wiped (consistent with say the Notes app, except if you disable iCloud it warns you that it'll remove those notes). When I re-enabled iCloud, the data appeared. (all done through the Settings app) However, in scenarios when NSPersistentCloudKitContainer cannot immediately sync -- say due to rate throttling -- and one disables iCloud in Settings, this wipes the local data store and ultimately results in data loss. This occurs even if the changes to the managed objects are saved (to the local store) -- it's simply they aren't synced in time. It can be a little hard to reproduce the is
Replies
3
Boosts
0
Views
375
Activity
Jan ’26
Reply to unifiedContacts identifier vs contactRelations identifier
Here's the test code where I was attempting to find the relationship between records. // ContentView.swift // TestContactsProject // import SwiftUI import Contacts import OSLog extension Logger { private static var subsystem = Bundle.main.bundleIdentifier ?? com.myelin.TestContactsProject static let logic = Logger(subsystem: subsystem, category: logic) static let analitics = Logger(subsystem: subsystem, category: analitics) } struct ContentView: View { var body: some View { Text(Hello, World!) .padding() .onAppear { Task.init { await fetchAllContacts() //await fetchSpecificContacts() } } } func fetchSpecificContacts(keyStrings: [String]) { // Run this in the background async // Get access to the Contacts Store let store = CNContactStore() // Keys to fetch let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey, CNContactIdentifierKey, CNContactRelationsKey] as [CNKeyDescriptor] // Search criteria let predicate = CNContact.predicateForContacts(withIdentifiers: keyStrings) d
Topic: App & System Services SubTopic: General Tags:
Replies
Boosts
Views
Activity
Dec ’25
Reply to Bug apple Health
// // OnboardingContainerView.swift // Worko // // Created by Rodrigo Soares on 20/12/25. // import SwiftUI import SwiftData /// Container que gerencia todo o fluxo de navegação do onboarding struct OnboardingContainerView: View { // MARK: - Properties @Environment(AppStateManager.self) private var appState @Environment(.modelContext) private var modelContext @State private var viewModel = OnboardingViewModel() @State private var isHealthKitReady = false // MARK: - Body var body: some View { ZStack { WorkoColors.background.ignoresSafeArea() // Content com transições ZStack { currentStepView .transition(.creativeSlide(direction: viewModel.navigationDirection)) } .animation(.spring(response: 0.6, dampingFraction: 0.8, blendDuration: 0), value: viewModel.currentStep) // Controles fixos (header + botão) if viewModel.currentStep != .completion { controlsOverlay } } .task { await viewModel.requestHealthKitAuthorization() isHealthKitReady = true } } // MARK: - Current Step View @ViewBuilder private var curr
Replies
Boosts
Views
Activity
Dec ’25
SwiftData .autosaveEnabled / rollback() trouble
Hello, In my iOS/SwiftUI/SwiftData app, I want the user to be able to hit [Cancel] from editing in a detail screen and return to the previous screen without changes being saved. I believed that setting autosaveEnabled to false and/or calling .rollback would prevent changes from being saved, unless/until I call .save() when the user clicks [Save], but this does not seem to be correct. I set modelContext.autosaveEnabled = false and I call modelContext.rollback() when the user hits [Cancel], but any changes they made are not rolled back, but saved even if I don’t call save(). I have tried setting autosaveEnabled to false when I create the ModelContainer on a @MainActor function when the App starts, and in the detail/edit screen’s .onAppear(). I can see that .rollback is being called when the [Cancel] button is tapped. In all cases, any changes the user made before hitting [Cancel] are saved. The Developer Documentation on autosaveEnabled includes this: “The default value is false. SwiftData aut
Replies
4
Boosts
0
Views
263
Activity
Dec ’25
Reply to NSWindowController subclass in Swift
You need to add override init(window: NSWindow?) { super.init(window: window) } in your subclass. See https://docs.swift.org/swift-book/documentation/the-swift-programming-language/initialization/ which say: Rule 1 If your subclass doesn’t define any designated initializers, it automatically inherits all of its superclass designated initializers. Rule 2 If your subclass provides an implementation of all of its superclass designated initializers — either by inheriting them as per rule 1, or by providing a custom implementation as part of its definition — then it automatically inherits all of the superclass convenience initializers. The (unstated) corollary here is that if you only override some of the superclass's designated initializers, you inherit none of them. in your case, you'd overridden init(coder:) not init(window:), so the compiler didn't 'see' an init(window:) at all. I'm not smart enough to figure this out on my own, I asked an LLM and then asked it to show me th
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Dec ’25