I have used core data before via the model editor. This is the first time I'm using swift data and that too with CloudKit. Can you tell me if the following model classes are correct?
I have an expense which can have only one sub category which in turn belongs to a single category. Here are my classes...
// Expense.swift
// Pocket Expense Diary
//
// Created by Neerav Kothari on 16/05/25.
//
import Foundation
import SwiftData
@Model
class Expense {
@Attribute var expenseDate: Date? = nil
@Attribute var expenseAmount: Double? = nil
@Attribute var expenseCategory: Category? = nil
@Attribute var expenseSubCategory: SubCategory? = nil
var date: Date {
get {
return expenseDate ?? Date()
}
set {
expenseDate = newValue
}
}
var amount: Double{
get {
return expenseAmount ?? 0.0
}
set {
expenseAmount = newValue
}
}
var category: Category{
get {
return expenseCategory ?? Category.init(name: "", icon: "")
}
set {
expenseCategory = newValue
}
}
var subCategory: SubCategory{
get {
return expenseSubCategory ?? SubCategory.init(name: "", icon: "")
}
set {
expenseSubCategory = newValue
}
}
init(date: Date, amount: Double, category: Category, subCategory: SubCategory) {
self.date = date
self.amount = amount
self.category = category
self.subCategory = subCategory
}
}
//
// Category.swift
// Pocket Expense Diary
//
// Created by Neerav Kothari on 16/05/25.
//
import Foundation
import SwiftData
@Model
class Category {
@Attribute var categoryName: String? = nil
@Attribute var categoryIcon: String? = nil
var name: String {
get {
return categoryName ?? ""
}
set {
categoryName = newValue
}
}
var icon: String {
get {
return categoryIcon ?? ""
}
set {
categoryIcon = newValue
}
}
@Relationship(inverse: \Expense.expenseCategory) var expenses: [Expense]? = []
init(name: String, icon: String) {
self.name = name
self.icon = icon
}
}
// SubCategory.swift
// Pocket Expense Diary
//
// Created by Neerav Kothari on 16/05/25.
//
import Foundation
import SwiftData
@Model
class SubCategory {
@Attribute var subCategoryName: String? = nil
@Attribute var subCategoryIcon: String? = nil
var name: String {
get {
return subCategoryName ?? ""
}
set {
subCategoryName = newValue
}
}
var icon: String {
get {
return subCategoryIcon ?? ""
}
set {
subCategoryIcon = newValue
}
}
@Relationship(inverse: \Expense.expenseSubCategory) var expenses: [Expense]? = []
init(name: String, icon: String) {
self.name = name
self.icon = icon
}
}
The reason why I have wrappers is the let the existing code (before CloudKit was integrated), work.
In future versions I plan to query expenses even via category or sub category. I particularly doubt for the relationship i have set. should there be one from category to subcategory as well?
SwiftData
RSS for tagSwiftData is an all-new framework for managing data within your apps. Models are described using regular Swift code, without the need for custom editors.
Posts under SwiftData tag
200 Posts
Sort by:
Post
Replies
Boosts
Views
Activity
Hi, I am building an iOS app with SwiftUI and SwiftData for the first time and I am experiencing a lot of difficulty with this error:
Thread 44: Fatal error: Never access a full future backing data - PersistentIdentifier(id: SwiftData.PersistentIdentifier.ID(backing: SwiftData.PersistentIdentifier.PersistentIdentifierBacking.managedObjectID(<ID> <x-coredata://<UUID>/MySwiftDataModel/p1>)), backing: SwiftData.PersistentIdentifier.PersistentIdentifierBacking.managedObjectID(<ID> <x-coredata://<UUID>/MySwiftDataModel/p1>)) with Optional(<UUID>)
I have been trying to figure out what the problem is, but unfortunately I cannot find any information in the documentation or on other sources online. My only theory about this error is that it is somehow related to fetching an entity that has been created in-memory, but not yet saved to the modelContext in SwiftData.
However, when I am trying to debug this, it's not clear this is the case. Sometimes the error happens, sometimes it doesn't. Saving manually does not always solve the error.
Therefore, it would be extremely helpful if someone could explain what this error means and whether there are any best practices to do with SwiftData, or some pitfalls to avoid (such as wrapping my model context into a repository class).
To be clear, this problem is NOT related to one area of my code, it happens throughout my app, at unpredictable places and time. Given that there is very little information related to this error, I am at a loss at how to make sure that this never happens.
This question has been asked on the forum here as well as on StackOverflow, Reddit (can't link that here), but none of the answers worked for me.
For reference, my models generally look like this:
import Foundation
import SwiftData
@Model
final class MySwiftDataModel {
// Stable cross-device identity
@Attribute(.unique)
var uuid: UUID
var someNumber: Int
var someString: String
@Relationship(deleteRule: .nullify, inverse: \AnotherSwiftDataModel.parentModel)
var childModels: [AnotherSwiftDataModel]
init(uuid: UUID = UUID(), someNumber: Int = 1, someString: String = "Some", childModels: [AnotherSwiftDataModel] = []) {
self.uuid = uuid
self.someNumber = someNumber
self.someString = someString
self.childModels = childModels
}
func addChildModel(model: AnotherSwiftDataModel) {
self.childModels.append(model)
}
func removeChildModel(by id: PersistentIdentifier) {
self.childModels = self.childModels.filter { $0.id != id }
}
}
and the child model:
import Foundation
import SwiftData
@Model
final class AnotherSwiftDataModel {
// Stable cross-device identity
@Attribute(.unique)
var uuid: UUID
var someNumber: Int
var someString: String
var parentModel: MySwiftDataModel?
init(uuid: UUID = UUID(), someNumber: Int = 1, someString: String = "Some") {
self.uuid = uuid
self.someNumber = someNumber
self.someString = someString
}
}
For now, you can assume I am not using CloudKit - i know for a fact the error is unrelated to CloudKit, because it happens when I am not using CloudKit (so I do not need to follow CloudKit's requirements for model design, such as nullable values etc).
As I said, the error surfaces at different times - sometimes during assignments, a lot of times during deletions of related models, etc.
Could you please explain what I am doing wrong and how I can make sure that this error does not happen? What are the architectural patterns that work best for SwiftData in this case? Do you have any examples of things I should avoid?
Thanks
Hello, thank you Apple for supporting custom store with SwiftData and the Schema type is superb to work with. I have successfully set one up with SQL and have some feedback and issues regarding its APIs.
There’s a highlighted message in the documentation about not using internal restricted symbols directly, but they contradict with the given protocols and I am concerned about breaking any App Store rules. Are we allowed to use these? If not, they should be opened up as they’re useful.
BackingData is required to set up custom snapshots, initialization, and getting/setting values. And I want to use it with createBackingData() to directly initialize instances from snapshots when transferring them between server and client or concurrency.
RelationshipCollection for casting to-many relationships from backing data or checking if an array contains a PersistentModel.
SchemaProperty for type erasure in a collection.
Schema.Relationship has KeyPath properties, but it is missing for Schema.Attribute and Schema.CompositeAttribute. Which means you can’t purely depend on the schema to map data. I am unable to access the properties of a custom struct type in a predicate unless I use Mirror with schemaMetadata() or CustomStringConvertible on the KeyPath directly to extract it.
Trivial, but… the KeyPath property name is inconsistent (it’s all lowercase).
It would be nice to retrieve property names from custom struct types, since you are unable access CodingKeys that are auto synthesized by Codable for structs. But I recently realized they’re a part Schema.CompositeAttribute, however I don’t know how to match these without the KeyPath…
I currently map my entities using CodingKeys to their PredicateCodableKeyPathProviding.… but I wish for a simpler alternative!
It’s unclear how to provide the schema to the snapshot before new models are created.
I currently use a static property, but I want to make it flexible if more schemas and configurations are added later on.
I considered saving and loading the schema in a temporary location, but doubtful that the KeyPath values will be available as they are not Codable.
I suspect schemaMetadata() has the information I need to map the backing data without a schema for snapshots, but as mentioned previously, properties are inaccessible…
Allow access to entity metatypes, like value types from SchemaProperty. They’re useful for getting data out of snapshots and casting them to CodingKeys and PredicateCodableKeyPathProviding. They do not carry over when you provide them in the Schema.
I am unable to retrieve the primary key from PersistentIdentifier.
It seems like once you create one, you can’t get it out, like the DataStoreConfiguration in ModelContainer is not the one you used to set it up. I cannot cast it, it is an entirely different struct?
I have to use JSONSerialization to extract it, but I want to get it directly since it is not a column in my database. It is transformed when it goes to/from my tables.
It’s unknown how to support some schema options, such as Spotlight and CloudKit.
Allow for extending macro options, such as adding options to set as primary key, whether to auto increment, etc…
You can create a schema for super and sub entities, but it doesn’t appear you can actually set them up from the @Model macro or use inheritance on these models…
SwiftData history tracking seems incomplete for HistoryDelete, because that protocol requires HistoryTombstone, but this type cannot be instantiated, nor does it contain anything useful to infer from.
As an aside, I want to create my own custom ModelActor that is a global actor. However, I’m unable to replicate the executor that Apple provides where the executor has a ModelContext, because this type does not conform to Sendable. So how did Apple do this? The documentation doesn’t mention unchecked Sendable, but I figure if the protocol is available then we would be able to set up our own.
And please add concurrency features!
Anyway, I hope for more continued support in the future and I am looking forward to what’s new this WWDC! 😊
Hi,
I am developing a Screen Time App and I am having issues with the ShieldConfigurationExtension (ShieldConfigurationDataSource). I know this extensions is sandboxed but I should be able to read data from the main app.
I am using SwiftData as my database, but I am unable to initialize it in the extensions with an error indicating insufficient file permissions. I have App Group set up and I am able to share data using UserDefaults but that is just inconvenient.
Is there any way I could just open the SwiftData in read only mode so that I could display the user some info on the shield?
SwiftData Init:
private func setupContainer() throws {
let schema = Schema([
DogEntity.self,
HouseEntity.self
])
// Use app group container if available
let config: ModelConfiguration
if let containerURL = FileManager.default.containerURL(
forSecurityApplicationGroupIdentifier: "group.\(Bundle.app.bundleIdentifier ?? "")"
) {
config = ModelConfiguration(schema: schema, url: containerURL.appendingPathComponent("default.sqlite"))
} else {
config = ModelConfiguration(schema: schema)
}
self.container = try ModelContainer(for: schema, configurations: [config])
}
Error in extension:
fault: Attempt to add read-only file at path file:///private/var/mobile/Containers/Shared/AppGroup/51431199-5919-4AE6-940C-6FE3C53EEB46/default.sqlite read/write. Adding it read-only instead. This will be a hard error in the future; you must specify the NSReadOnlyPersistentStoreOption.
error: (3) access permission denied
error: Encountered exception error during prepareSQL for SQL string 'SELECT TBL_NAME FROM SQLITE_MASTER WHERE TBL_NAME = 'Z_METADATA'' : access permission denied with userInfo {
NSFilePath = "/private/var/mobile/Containers/Shared/AppGroup/51431199-5919-4AE6-940C-6FE3C53EEB46/default.sqlite";
NSSQLiteErrorDomain = 3;
} while checking table name from store: <NSSQLiteConnection: 0x154100300>
error: Store failed to load. <NSPersistentStoreDescription: 0x15402d590> (type: SQLite, url: file:///private/var/mobile/Containers/Shared/AppGroup/51431199-5919-4AE6-940C-6FE3C53EEB46/default.sqlite) with error = Error Domain=NSCocoaErrorDomain Code=256 "The file “default.sqlite” couldn’t be opened." UserInfo={NSFilePath=/private/var/mobile/Containers/Shared/AppGroup/51431199-5919-4AE6-940C-6FE3C53EEB46/default.sqlite, NSSQLiteErrorDomain=3} with userInfo {
NSFilePath = "/private/var/mobile/Containers/Shared/AppGroup/51431199-5919-4AE6-940C-6FE3C53EEB46/default.sqlite";
NSSQLiteErrorDomain = 3;
}
Any help appreciated 🙂
Topic:
App & System Services
SubTopic:
General
Tags:
Family Controls
Managed Settings
Screen Time
SwiftData
I am following Apple's instruction to sync SwiftData with CloudKit. While initiating the ModelContainer, right after removing the store from Core Data, the error occurs:
FAULT: NSInternalInconsistencyException: This NSPersistentStoreCoordinator has no persistent stores (unknown). It cannot perform a save operation.; (user info absent)
I've tried removing default.store and its related files/folders before creating the ModelContainer with FileManager but it does not resolve the issue. Isn't it supposed to create a new store when the ModelContainer is initialized? I don't understand why this error occurs. Error disappears when I comment out the #if DEBUG block.
Code:
import CoreData
import SwiftData
import SwiftUI
struct InitView: View {
@Binding var modelContainer: ModelContainer?
@Binding var isReady: Bool
@State private var loadingDots = ""
@State private var timer: Timer?
var body: some View {
VStack(spacing: 16) {
Text("Loading\(loadingDots)")
.font(.title2)
.foregroundColor(.gray)
}
.padding()
.onAppear {
startAnimation()
registerTransformers()
let config = ModelConfiguration()
let newContainer: ModelContainer
do {
#if DEBUG
// Use an autorelease pool to make sure Swift deallocates the persistent
// container before setting up the SwiftData stack.
try autoreleasepool {
let desc = NSPersistentStoreDescription(url: config.url)
let opts = NSPersistentCloudKitContainerOptions(containerIdentifier: "iCloud.my-container-identifier")
desc.cloudKitContainerOptions = opts
// Load the store synchronously so it completes before initializing the
// CloudKit schema.
desc.shouldAddStoreAsynchronously = false
if let mom = NSManagedObjectModel.makeManagedObjectModel(for: [Page.self]) {
let container = NSPersistentCloudKitContainer(name: "Pages", managedObjectModel: mom)
container.persistentStoreDescriptions = [desc]
container.loadPersistentStores { _, err in
if let err {
fatalError(err.localizedDescription)
}
}
// Initialize the CloudKit schema after the store finishes loading.
try container.initializeCloudKitSchema()
// Remove and unload the store from the persistent container.
if let store = container.persistentStoreCoordinator.persistentStores.first {
try container.persistentStoreCoordinator.remove(store)
}
}
// let fileManager = FileManager.default
// let sqliteURL = config.url
// let urls: [URL] = [
// sqliteURL,
// sqliteURL.deletingLastPathComponent().appendingPathComponent("default.store-shm"),
// sqliteURL.deletingLastPathComponent().appendingPathComponent("default.store-wal"),
// sqliteURL.deletingLastPathComponent().appendingPathComponent(".default_SUPPORT"),
// sqliteURL.deletingLastPathComponent().appendingPathComponent("default_ckAssets")
// ]
// for url in urls {
// try? fileManager.removeItem(at: url)
// }
}
#endif
newContainer = try ModelContainer(for: Page.self,
configurations: config) // ERROR!!!
} catch {
fatalError(error.localizedDescription)
}
modelContainer = newContainer
isReady = true
}
.onDisappear {
stopAnimation()
}
}
private func startAnimation() {
timer = Timer.scheduledTimer(
withTimeInterval: 0.5,
repeats: true
) { _ in
updateLoadingDots()
}
}
private func stopAnimation() {
timer?.invalidate()
timer = nil
}
private func updateLoadingDots() {
if loadingDots.count > 2 {
loadingDots = ""
} else {
loadingDots += "."
}
}
}
import CoreData
import SwiftData
import SwiftUI
@main
struct MyApp: App {
@State private var modelContainer: ModelContainer?
@State private var isReady: Bool = false
var body: some Scene {
WindowGroup {
if isReady, let modelContainer = modelContainer {
ContentView()
.modelContainer(modelContainer)
} else {
InitView(modelContainer: $modelContainer, isReady: $isReady)
}
}
}
}
Hi all,
I’m encountering a consistent issue with SwiftData on watchOS when using CloudKit sync. After enabling:
let config = ModelConfiguration(schema: schema, cloudKitDatabase: .automatic)
…the app terminates ~30–60 seconds into a WKExtendedRuntimeSession. This happens specifically when:
Always-On Display is OFF
The iPhone is disconnected or in Airplane Mode
The app is running in a WKExtendedRuntimeSession (e.g., used for meditation tracking)
The Xcode logs show a warning:
Background Task ("CoreData: CloudKit Setup"), was created over 30 seconds ago. In applications running in the background, this creates a risk of termination.
It appears CloudKit sync setup is being triggered automatically and flagged by the system as an unmanaged long-running task, leading to termination.
Workaround:
Switching to:
let config = ModelConfiguration(schema: schema, cloudKitDatabase: .none)
…prevents the issue entirely — no background task warning, no crash.
Feedback ID submitted: FB17685611
Just wanted to check if others have seen this behavior or found alternative solutions. It seems like something Apple may need to address in SwiftData’s CloudKit handling on watchOS.
Topic:
App & System Services
SubTopic:
iCloud & Data
Tags:
CloudKit
watchOS
Background Tasks
SwiftData
My project is using swiftData and I want to implement iCloud sync in it. Now, my data base doesnt have any optional attributes or relationships and CloudKit wants them to be optional.
So, rather than editing all code with unwrapping code for the optionals, how can I provide a bridge that does so in the last stage of actually saving to the store? Sort of, capture it in a proxy object before writing and after reading from the store.
Is there a neat way that can save a lot of debugging? I have code snippets from chat gpt and they are hard to debug. This is my first project in swiftUI.
Thanks.
Neerav
Issue with SwiftData: “no such table: ZAPPSETTINGS” and SQLite I/O error on app launch
Hello,
I’m encountering persistent errors with SwiftData in my SwiftUI app related to Core Data’s underlying SQLite database. Despite defining my models correctly, the app fails to initialize the persistent store, throwing the following error on startup:
CoreData error: SQLCore dispatchRequest: no such table: ZAPPSETTINGS. I/O error opening database at /.../default.store. SQLite error code:1, NSSQLiteErrorDomain=1.
File “default.store” couldn’t be opened.
Context
The error only appears concerning my AppSettings model.
I have another model, LocationPoint, which appears correctly defined and used.
I have tried deleting the app, resetting the device, and cleaning builds but the error persists.
The error message suggests the database file is present but the table for ZAPPSETTINGS (the Core Data table for AppSettings) does not exist.
Code Samples
Main App Entry
import SwiftData
import SwiftUI
@main
struct Krow3_0App: App {
@State private var userLocationManager = UserLocationManager()
@State private var geocodingViewModel = GeocodingViewModel()
@State private var locationSearchViewModel = LocationSearchViewModel()
@State private var router = Router()
var body: some Scene {
WindowGroup {
LaunchView()
.environment(userLocationManager)
.environment(geocodingViewModel)
.environment(locationSearchViewModel)
.environment(router)
.modelContainer(for: [LocationPoint.self, AppSettings.self])
}
}
}
AppSettings Model
import Foundation
import SwiftData
@Model
class AppSettings {
var isMetric: Bool
init(isMetric: Bool = false) {
self.isMetric = isMetric
}
}
What I’ve Tried
Fully uninstalling and reinstalling the app on device and simulator.
Resetting the simulator/device.
Cleaning the Xcode build folder.
Verifying the schema logs which correctly list both LocationPoint and AppSettings.
Changing model names to avoid potential conflicts.
Adding .modelContainer configuration with autosave enabled.
Questions
Is there a known bug or limitation with SwiftData concerning certain model setups or naming?
Could this be related to how the data container initializes or migrates schemas?
Are there recommended debugging or migration steps to resolve “no such table” SQLite errors with SwiftData?
How can I safely reset or migrate the persistent store without corrupting the database?
Any insights or suggestions would be greatly appreciated!
Thank you!
I'm writing some tests to confirm the behavior of my app. White creating a model actor to delete objects I realized that ModelContext.model(for:) does return objects that are deleted. I was able to reproduces this with this minimal test case:
@Model class Activity {
init() {}
}
struct MyLibraryTests {
let modelContainer = try! ModelContainer(
for: Activity.self,
configurations: ModelConfiguration(
isStoredInMemoryOnly: true
)
)
init() throws {
let context = ModelContext(modelContainer)
context.insert(Activity())
try context.save()
}
@Test func modelForIdAfterDelete() async throws {
let context = ModelContext(modelContainer)
let id = try context.fetch(FetchDescriptor<Activity>()).first!.id
context.delete(context.model(for: id) as! Activity)
try context.save()
let result = context.model(for: id) as? Activity
#expect(result == nil) // Expectation failed: (result → MyLibrary.Activity) == nil
}
@Test func fetchDescriptorAfterDelete() async throws {
let context = ModelContext(modelContainer)
let id = try context.fetch(FetchDescriptor<Activity>()).first!.id
context.delete(context.model(for: id) as! Activity)
try context.save()
let result = try context.fetch(
FetchDescriptor<Activity>(predicate: #Predicate { $0.id == id })
).first
#expect(result == nil)
}
}
Here I create a new context, insert an model and save it.
The test modelForIdAfterDelete does fail, as result still contains the deleted object.
I also tried to check #expect(result!.isDeleted), but it is also false.
With the second test I use a FetchDescriptor to retrieve the object by ID and it correctly returns nil.
Shouldn't both methods use a consistent behavior?
I'm using SwiftData with CloutKit with a very simple app. Data syncs between iOS, iPadOS, and visionOS, but not macOS. From what I can tell, macOS is never getting CK messages unless I'm running the app from Xcode.
I can listen for the CK messages and show a line in a debug overlay. This works perfectly when I run from Xcode. I can see the notifications and see updates in my app. However, if I just launch the app outside of Xcode I will never see any changes or notifications. It is as if the Mac app never even tries to contact CloudKit.
Schema has been deployed in the CloudKit console. The app is based on the multi-platform Xcode template. Again, only the macOS version has this issue. Is there some extra permission or setting I need to set up in order to use CloudKit on macOS?
@State private var publisher = NotificationCenter.default.publisher(for: NSPersistentCloudKitContainer.eventChangedNotification).receive(on: DispatchQueue.main)
.onReceive(publisher) { notification in
// Listen for changes in CK events
if let userInfo = notification.userInfo,
let event = userInfo[NSPersistentCloudKitContainer.eventNotificationUserInfoKey] as? NSPersistentCloudKitContainer.Event {
let message = "CloudKit Sync: \(event.type.rawValue) - \(event.succeeded ? "Success" : "Failed") - \(event.description)"
// Store for UI display
syncNotifications.append(message)
if syncNotifications.count > 10 {
syncNotifications.removeFirst()
}
}
}
.overlay(alignment: .topTrailing) {
if !syncNotifications.isEmpty {
VStack(alignment: .leading) {
ForEach(syncNotifications, id: \.self) { notification in
Text(notification)
.padding(8)
}
}
.frame(width: 800, height: 500)
.cornerRadius(8)
.background(Color.secondary.opacity(0.2))
.padding()
.transition(.move(edge: .top))
}
}
Hi !
Would anyone know (if possible) how to create backup files to export and then import from the data recorded by SwiftData?
For those who wish, here is a more detailed explanation of my case:
I am developing a small management software with customers and events represented by distinct classes. I would like to have an "Export" button to create a file with all the instances of these 2 classes and another "Import" button to replace all the old data with the new ones from a previously exported file.
I looked for several solutions but I'm a little lost...
SwiftData crashes 100% when fetching history of a model that contains an optional codable property that's updated:
SwiftData/Schema.swift:389: Fatal error: Failed to materialize a keypath for someCodableID.someID from CrashModel. It is possible that this path traverses a type that does not work with append(), please file a bug report with a test.
Would really appreciate some help or even a workaround.
Code:
import Foundation
import SwiftData
import Testing
struct VaultsSwiftDataKnownIssuesTests {
@Test
func testCodableCrashInHistoryFetch() async throws {
let container = try ModelContainer(
for: CrashModel.self,
configurations: .init(
isStoredInMemoryOnly: true
)
)
let context = ModelContext(container)
try SimpleHistoryChecker.hasLocalHistoryChanges(context: context)
// 1: insert a new value and save
let model = CrashModel()
model.someCodableID = SomeCodableID(someID: "testid1")
context.insert(model)
try context.save()
// 2: check history it's fine.
try SimpleHistoryChecker.hasLocalHistoryChanges(context: context)
// 3: update the inserted value before then save
model.someCodableID = SomeCodableID(someID: "testid2")
try context.save()
// The next check will always crash on fetchHistory with this error:
/*
SwiftData/Schema.swift:389: Fatal error: Failed to materialize a keypath for someCodableID.someID from CrashModel. It is possible that this path traverses a type that does not work with append(), please file a bug report with a test.
*/
try SimpleHistoryChecker.hasLocalHistoryChanges(context: context)
}
}
@Model final class CrashModel {
// optional codable crashes.
var someCodableID: SomeCodableID?
// these actually work:
//var someCodableID: SomeCodableID
//var someCodableID: [SomeCodableID]
init() {}
}
public struct SomeCodableID: Codable {
public let someID: String
}
final class SimpleHistoryChecker {
static func hasLocalHistoryChanges(context: ModelContext) throws {
let descriptor = HistoryDescriptor<DefaultHistoryTransaction>()
let history = try context.fetchHistory(descriptor)
guard let last = history.last else {
return
}
print(last)
}
}
I have a SwiftData document-based app. It is initialized like this:
@main
struct MyApp: App {
@State private var showTemplatePicker = false
@State private var documentCreationContinuation: CheckedContinuation<URL?, any Error>?
var body: some Scene {
DocumentGroup(editing: .myDocument, migrationPlan: MyMigrationPlan.self) {
CanvasView()
}
DocumentGroupLaunchScene(Text("My App")) {
NewDocumentButton("New", contentType: .canvasDocument) {
try await withCheckedThrowingContinuation { continuation in
documentCreationContinuation = continuation
showTemplatePicker = true
}
}
.fullScreenCover(isPresented: $showTemplatePicker) {
TemplateView(documentCreationContinuation: $documentCreationContinuation)
}
} background: {
Image("BoardVignette")
.resizable()
}
}
}
extension UTType {
static var canvasDocument: UTType {
UTType(importedAs: "com.example.MyApp.canvas")
}
}
Pressing the New button crashes with:
#0 0x00000001d3a6e12c in (1) suspend resume partial function for closure #1 () async -> () in SwiftUI.IdentifiedDocumentGroupDocumentCreation.createNewDocument(with: SwiftUI.IdentifiedDocumentGroupConfiguration, url: Swift.Optional<Foundation.URL>, newDocumentProvider: Swift.Optional<SwiftUI.AsyncNewDocumentProvider>, _: (Swift.Optional<SwiftUI.PlatformDocument>) -> ()) -> () ()
All sample code that I've seen uses a FileDocument but SwiftData's setup doesn't have one so it's not completely clear how you should be using NewDocumentButton with a SwiftData file.
The crash happens even before my prepareDocumentURL handler is called (I set a breakpoint and it never stops). My hunch is that the crash is because it's not able to match my contentType to a Document. Can anyone at Apple help? I don't think this use-case has been documented well.
I am using SwiftData to model my data. For that i created a model called OrganizationData that contains various relationships to other entities. My data set is quite large and i am having a big performance issue when fetching all OrganizationData entities. I started debugging and looking at the sql debug log i noticed that when fetching my entities i run into faults for all relationships even when not accessing them.
Fetching my entities:
let fetchDescriptor = FetchDescriptor<OrganizationData>()
let context = MapperContext(dataManager: self)
let organizations = (try modelContainer.mainContext.fetch(fetchDescriptor))
Doing this fetch, also fetches all relationships. Each in a single query, for every OrganizationData entity.
CoreData: annotation: to-many relationship fault "relationship1" for objectID 0x8aa5249772916e00 <x-coredata://B891FCEB-DF16-4E11-98E6-0AFB5D171A81/OrganizationData/p3869> fulfilled from database. Got 9 rows
CoreData: annotation: to-many relationship fault "relationship2" for objectID 0x8aa5249772916e00 <x-coredata://B891FCEB-DF16-4E11-98E6-0AFB5D171A81/OrganizationData/p3869> fulfilled from database. Got 0 rows
CoreData: annotation: to-many relationship fault "relationship3" for objectID 0x8aa5249772916e00 <x-coredata://B891FCEB-DF16-4E11-98E6-0AFB5D171A81/OrganizationData/p3869> fulfilled from database. Got 0 rows
CoreData: annotation: to-many relationship fault "relationship4" for objectID 0x8aa5249772916e00 <x-coredata://B891FCEB-DF16-4E11-98E6-0AFB5D171A81/OrganizationData/p3869> fulfilled from database. Got 0 rows
CoreData: annotation: to-many relationship fault "relationship5" for objectID 0x8aa5249772916e00 <x-coredata://B891FCEB-DF16-4E11-98E6-0AFB5D171A81/OrganizationData/p3869> fulfilled from database. Got 0 rows
CoreData: annotation: to-many relationship fault "relationship6" for objectID 0x8aa5249772916e00 <x-coredata://B891FCEB-DF16-4E11-98E6-0AFB5D171A81/OrganizationData/p3869> fulfilled from database. Got 0 rows
CoreData: annotation: to-many relationship fault "relationship7" for objectID 0x8aa5249772916e00 <x-coredata://B891FCEB-DF16-4E11-98E6-0AFB5D171A81/OrganizationData/p3869> fulfilled from database. Got 1 rows
CoreData: annotation: to-many relationship fault "relationship8" for objectID 0x8aa5249772916e00 <x-coredata://B891FCEB-DF16-4E11-98E6-0AFB5D171A81/OrganizationData/p3869> fulfilled from database. Got 0 rows
CoreData: annotation: to-many relationship fault "relationship9" for objectID 0x8aa5249772916e00 <x-coredata://B891FCEB-DF16-4E11-98E6-0AFB5D171A81/OrganizationData/p3869> fulfilled from database. Got 0 rows
The relationships are all defined the same
@Relationship(deleteRule: .cascade, inverse: \EntityData1.organization)
var relationship1: [EntityData1] = []
Am i missing something? As far as i understood relationships are lazy and should only be faulted when accessing the property. But doing the fetch as described above already causes a query to happen, making the fetch take very long when using a large data set.
I'm running into a crash when trying to delete an item from a list that's loaded using SwiftData. The app works fine when selecting or displaying the data, but the moment I confirm a deletion, it crashes with this error:
SwiftData/ModelSnapshot.swift:46: Fatal error: A ModelSnapshot must be initialized with a known-keys dictionary
This happens right after I delete an item from the list using modelContext.delete(). I’ve double-checked that the item exists and is valid, and I'm not sure what I'm doing wrong. The data is loaded using @Query and everything seems normal until deletion.
For further information, I have tried this on a new IOS project where I have one super Model class with a cascading relationship on a child class. When trying to delete the parent class while connected to one or more children, it still gives me the error.
The same thing is happening with my original project. Class A has a relationship (cascading) with Class B. Attempting to delete Class A while there are relationships with Class B throws this error.
If anyone has experienced this or knows what causes it, please let me know. I’m not even sure where to start debugging this one.
Thanks in advance!
The stuff I've found by searching has confused me, so hopefully someone can help simplify it for me?
I have an app (I use it for logging which books I've given away), and I could either add a bunch of things to the app, or I could have another app (possibly a CLI tool) to generate some reports I'd like.
I'm getting the following error message when executing the rollback method in a modelContext, what could be causing this ?
SwiftData/ModelSnapshot.swift:46: Fatal error: A ModelSnapshot must be initialized with a known-keys dictionary
Hello,
I am trying to get the elements from my SwiftData databse in the configuration for my widget.
The SwiftData model is the following one:
@Model
class CountdownEvent {
@Attribute(.unique) var id: UUID
var title: String
var date: Date
@Attribute(.externalStorage) var image: Data
init(id: UUID, title: String, date: Date, image: Data) {
self.id = id
self.title = title
self.date = date
self.image = image
}
}
And, so far, I have tried the following thing:
AppIntent.swift
struct ConfigurationAppIntent: WidgetConfigurationIntent {
static var title: LocalizedStringResource { "Configuration" }
static var description: IntentDescription { "This is an example widget." }
// An example configurable parameter.
@Parameter(title: "Countdown")
var countdown: CountdownEntity?
}
Countdowns.swift, this is the file with the widget view
struct Provider: AppIntentTimelineProvider {
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), configuration: ConfigurationAppIntent())
}
func snapshot(for configuration: ConfigurationAppIntent, in context: Context) async -> SimpleEntry {
SimpleEntry(date: Date(), configuration: configuration)
}
func timeline(for configuration: ConfigurationAppIntent, in context: Context) async -> Timeline<SimpleEntry> {
var entries: [SimpleEntry] = []
// Generate a timeline consisting of five entries an hour apart, starting from the current date.
let currentDate = Date()
for hourOffset in 0 ..< 5 {
let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
let entry = SimpleEntry(date: entryDate, configuration: configuration)
entries.append(entry)
}
return Timeline(entries: entries, policy: .atEnd)
}
// func relevances() async -> WidgetRelevances<ConfigurationAppIntent> {
// // Generate a list containing the contexts this widget is relevant in.
// }
}
struct SimpleEntry: TimelineEntry {
let date: Date
let configuration: ConfigurationAppIntent
}
struct CountdownsEntryView : View {
var entry: Provider.Entry
var body: some View {
VStack {
Text("Time:")
Text(entry.date, style: .time)
Text("Title:")
Text(entry.configuration.countdown?.title ?? "Default")
}
}
}
struct Countdowns: Widget {
let kind: String = "Countdowns"
var body: some WidgetConfiguration {
AppIntentConfiguration(kind: kind, intent: ConfigurationAppIntent.self, provider: Provider()) { entry in
CountdownsEntryView(entry: entry)
.containerBackground(.fill.tertiary, for: .widget)
}
}
}
CountdownEntity.swift, the file for the AppEntity and EntityQuery structs
struct CountdownEntity: AppEntity, Identifiable {
var id: UUID
var title: String
var date: Date
var image: Data
var displayRepresentation: DisplayRepresentation {
DisplayRepresentation(title: "\(title)")
}
static var defaultQuery = CountdownQuery()
static var typeDisplayRepresentation: TypeDisplayRepresentation = "Countdown"
init(id: UUID, title: String, date: Date, image: Data) {
self.id = id
self.title = title
self.date = date
self.image = image
}
init(id: UUID, title: String, date: Date) {
self.id = id
self.title = title
self.date = date
self.image = Data()
}
init(countdown: CountdownEvent) {
self.id = countdown.id
self.title = countdown.title
self.date = countdown.date
self.image = countdown.image
}
}
struct CountdownQuery: EntityQuery {
typealias Entity = CountdownEntity
static var typeDisplayRepresentation = TypeDisplayRepresentation(name: "Countdown Event")
static var defaultQuery = CountdownQuery()
@Environment(\.modelContext) private var modelContext // Warning here: Stored property '_modelContext' of 'Sendable'-conforming struct 'CountdownQuery' has non-sendable type 'Environment<ModelContext>'; this is an error in the Swift 6 language mode
func entities(for identifiers: [UUID]) async throws -> [CountdownEntity] {
let countdownEvents = getAllEvents(modelContext: modelContext)
return countdownEvents.map { event in
return CountdownEntity(id: event.id, title: event.title, date: event.date, image: event.image)
}
}
func suggestedEntities() async throws -> [CountdownEntity] {
// Return some suggested entities or an empty array
return []
}
}
CountdownsManager.swift, this one just has the function that gets the array of countdowns
func getAllEvents(modelContext: ModelContext) -> [CountdownEvent] {
let descriptor = FetchDescriptor<CountdownEvent>()
do {
let allEvents = try modelContext.fetch(descriptor)
return allEvents
}
catch {
print("Error fetching events: \(error)")
return []
}
}
I have installed it in my phone and when I try to edit the widget, it doesn't show me any of the elements I have created in the app, just a loading dropdown for half a second:
What am I missing here?
Topic:
App & System Services
SubTopic:
Widgets & Live Activities
Tags:
SwiftUI
WidgetKit
App Intents
SwiftData
I'm trying out putting most of my business logic in a Protocol that my @Model can conform to, but I'm running into a SwiftUI problem with a Binding that does not get magically offered up like it does when it the subview is not generic.
I have a pretty basic List with a ForEach that now can't properly pass to a generic view based on a protocol. When I try to make a binding manually in the row it says that "item is immutable"... but that also doesn't help me with the NavigationLink? Which is seeing the Binding not the ? But before when the subview was concrete to Thing, it took in the and made its own Binding once it hit the view. I'm unclear on precisely where the change happens and what I can do to work around it.
Before I go rearchitecting everything... is there a fix to get the NavigationLink to take on the object like before? What needs to be different?
I've tried a number of crazy inits on the subview and they all seem to come back to saying either it can't figure out how to pass the type or I'm trying to use the value before it's been initialized.
Have I characterized the problem correctly?
Thanks!
(let me know if I forgot a piece of code, but this should be the List, the Model/Protocol and the subview)
import SwiftUI
import SwiftData
struct ThingsView: View {
@Environment(\.modelContext) var modelContext
@Query var items: [Thing]
var body: some View {
NavigationStack {
List {
ForEach(items) { item in
NavigationLink(value: item) {
VStack(alignment: .leading) {
Text(item.textInfo)
.font(.headline)
Text(item.timestamp.formatted(date: .long, time: .shortened))
}
}
}.onDelete(perform: deleteItems)
}
.navigationTitle("Fliiiing!")
//PROBLEM HERE: Cannot convert value of type '(Binding<Thing>) -> EditThingableView<Thing>' to expected argument type '(Thing) -> EditThingableView<Thing>'
.navigationDestination(for: Thing.self, destination: EditThingableView<Thing>.init)
#if os(macOS)
.navigationSplitViewColumnWidth(min: 180, ideal: 200)
#endif
.toolbar {
#if os(iOS)
ToolbarItem(placement: .navigationBarTrailing) {
EditButton()
}
#endif
ToolbarItem {
Button(action: addItem) {
Label("Add Item", systemImage: "plus")
}
}
ToolbarItem {
Button("Add Samples", action: addSamples)
}
}
}
}
func addSamples() {
withAnimation {
ItemSDMC.addSamples(context: modelContext)
}
}
private func addItem() {
withAnimation {
let newItem = ItemSDMC("I did a thing!")
modelContext.insert(newItem)
}
}
func deleteItems(_ indexSet:IndexSet) {
withAnimation {
for index in indexSet {
items[index].delete(from: modelContext)
}
}
}
}
#Preview {
ThingsView().modelContainer(for: ItemSDMC.self, inMemory: true)
}
import Foundation
import SwiftData
protocol Thingable:Identifiable {
var textInfo:String { get set }
var timestamp:Date { get set }
}
extension Thingable {
var thingDisplay:String {
"\(textInfo) with \(id) at \(timestamp.formatted(date: .long, time: .shortened))"
}
}
extension Thingable where Self:PersistentModel {
var thingDisplayWithID:String {
"\(textInfo) with modelID \(self.persistentModelID.id) in \(String(describing: self.persistentModelID.storeIdentifier)) at \(timestamp.formatted(date: .long, time: .shortened))"
}
}
struct ThingLite:Thingable, Codable, Sendable {
var textInfo: String
var timestamp: Date
var id: Int
}
@Model
final class Thing:Thingable {
//using this default value requires writng some clean up logic looking for empty text info.
var textInfo:String = ""
//using this default value would require writing some data clean up functions looking for out of bound dates.
var timestamp:Date = Date.distantPast
init(textInfo: String, timestamp: Date) {
self.textInfo = textInfo
self.timestamp = timestamp
}
}
extension Thing {
var LiteThing:ThingLite {
ThingLite(textInfo: textInfo, timestamp: timestamp, id: persistentModelID.hashValue)
}
}
import SwiftUI
struct EditThingableView<DisplayItemType:Thingable>: View {
@Binding var thingHolder: DisplayItemType
var body: some View {
VStack {
Text(thingHolder.thingDisplay)
Form {
TextField("text", text:$thingHolder.textInfo)
DatePicker("Date", selection: $thingHolder.timestamp)
}
}
#if os(iOS)
.navigationTitle("Edit Item")
.navigationBarTitleDisplayMode(.inline)
#endif
}
}
//NOTE: First sign of trouble
//#Preview {
// @Previewable var myItem = Thing(textInfo: "Example Item for Preview", timestamp:Date())
// EditThingableView<Thing>(thingHolder: myItem)
//}
Hi all,
I am using SwiftData and cloudkit and I am having an extremely persistent bug.
I am building an education section on a app that's populated with lessons via a local JSON file. I don't need this lesson data to sync to cloudkit as the lessons are static, just need them imported into swiftdata so I've tried to use the modelcontainer like this:
static func createSharedModelContainer() -> ModelContainer {
// --- Define Model Groups ---
let localOnlyModels: [any PersistentModel.Type] = [
Lesson.self, MiniLesson.self,
Quiz.self, Question.self
]
let cloudKitSyncModels: [any PersistentModel.Type] = [
User.self, DailyTip.self, UserSubscription.self,
UserEducationProgress.self // User progress syncs
]
However, what happens is that I still get Lesson and MiniLesson record types on cloudkit and for some reason as well, whenever I update the data models or delete and reinstall the app on simulator, the lessons duplicate (what seems to happen is that a set of lessons comes from the JSON file as it should), and then 1-2 seconds later, an older set of lessons gets synced from cloudkit.
I can delete the old set of lessons if I just delete the lessons and mini lessons record types, but if I update the data model again, this error reccurrs.
Sorry, I don't know if I managed to explain this well but essentially I just want to stop the lessons and minilessons from being uploaded to cloudkit as I think this will fix the problem. Am I doing something wrong with the code?