Core Data

RSS for tag

Save your application’s permanent data for offline use, cache temporary data, and add undo functionality to your app on a single device using Core Data.

Posts under Core Data tag

200 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

MacOS App Core Data app is stuck in "This NSPersistentStoreCoordinator has no persistent stores (schema mismatch or migration failure). It cannot perform a save operation."
I work on a MacOS app (which has a companion iOS app) that uses Core Data with NSPersistentCloudKitContainer. The app also supports widgets and hence there is a need to migrate the persistent store within the core data stack using replacePersistentStore( at:.... During development I recently created a new model version and added a new entity, replaced some attributes etc... Working on the iOS app is fine because deleting the app clears all the data allowing me to work with a clean slate. On MacOS, I initially thought that I could simply navigate to the app group file location, delete the .sqlite file, along with the sqlite-shm and sqlite-wal. I also went and deleted the CloudKit related files. I did all of this out of pure ignorance - my expectation was that it would give me a clean slate, but it did not. This instead gave me some unpredictable behaviour, but the app was always in a bad state. the issues I saw were; • migration failure, • sqlite errors highlighting no such column: t0 - where all the new entity details were missing in sqlite completely After finding a post in the forums about how to reset macOS correctly, I did this instead - do { try container.persistentStoreCoordinator.destroyPersistentStore(at: container.persistentStoreDescriptions.first!.url!, type: .sqlite, options: nil) try container.persistentStoreCoordinator.destroyPersistentStore(at: storeURL, type: .sqlite, options: nil) } catch { print(String(describing: error)) } And now I am back to the ongoing error of This NSPersistentStoreCoordinator has no persistent stores (schema mismatch or migration failure). It cannot perform a save operation. Another thing to note - whenever running the destroyPersistentStore( I have tried this on both the URLs of the old store location and the new one (in the app group). This still doesn't seem to help. AND I noticed that while destroyPersistentStore does get rid of the .sqlite file, it does not delete the sqlite-shm and sqlite-wal - could this be the problem? and do I need to delete these manually? public class CoreDataManager { public static let shared = CoreDataManager() private enum Constants { #if os(macOS) static let appGroupName = "2MM2V2959F.wabitime.group" #elseif os(iOS) static let appGroupName = "group.wabitime" #endif static let containerName = "WabiTimeDataModel" /// The name of the sql database file static let databaseName = "wabitime_database" /// The identifier for the container static let containerIdentifier = "iCloud.com.jslomowitz.WabiTime" } public lazy var context = persistentContainer.viewContext lazy var managedObjectModel: NSManagedObjectModel = { guard let wabiDataBundle = Bundle.module.url( forResource: Constants.containerName, withExtension: "momd" ), let managedObjectModel = NSManagedObjectModel(contentsOf: wabiDataBundle) else { assertionFailure("cannot find managedObjectModel") return NSManagedObjectModel() } return managedObjectModel }() lazy var persistentContainer: NSPersistentCloudKitContainer = { let container = NSPersistentCloudKitContainer( name: Constants.containerName, managedObjectModel: managedObjectModel ) /// URL of the old sql database that has not been relocated to the app group var oldStoreURL: URL? { guard let storeDescription = container.persistentStoreDescriptions.first, let url = storeDescription.url, FileManager.default.fileExists(atPath: url.path) else { return nil } return url } /// URL of the sql database in the app group var storeURL: URL { guard let fileContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: Constants.appGroupName) else { fatalError("Shared file container could not be created") } return fileContainer.appendingPathComponent("\(Constants.databaseName).sqlite") } // assign the shared container if the old store has been deleted if oldStoreURL == nil { let description = NSPersistentStoreDescription(url: storeURL) description.shouldInferMappingModelAutomatically = true description.shouldMigrateStoreAutomatically = true container.persistentStoreDescriptions = [description] } // perform store migration if necessary if let url = oldStoreURL, url.absoluteString != storeURL.absoluteString { let coordinator = container.persistentStoreCoordinator do { let storeOptions = [ NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true ] try coordinator.replacePersistentStore( at: url, withPersistentStoreFrom: storeURL, sourceOptions: storeOptions, type: .sqlite ) } catch { print(error.localizedDescription) } self.deleteOldStore(with: url) } let options = NSPersistentCloudKitContainerOptions(containerIdentifier: Constants.containerIdentifier) guard let description = container.persistentStoreDescriptions.first else { fatalError("Could not retrieve a persistent store description.") } description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey) description.cloudKitContainerOptions = options container.loadPersistentStores(completionHandler: { [weak self] (_, error) in guard let self, error == nil else { assertionFailure("Unresolved error: \(String(describing: error))") return } }) container.viewContext.automaticallyMergesChangesFromParent = true container.viewContext.mergePolicy = NSMergePolicy(merge: .mergeByPropertyObjectTrumpMergePolicyType) return container }() private func deleteOldStore(with url: URL) { let fileCoordinator = NSFileCoordinator() fileCoordinator.coordinate(writingItemAt: url, options: .forDeleting, error: nil) { url in do { try FileManager.default.removeItem(at: url) } catch { print(error.localizedDescription) } } } // MARK: - Core Data Saving and Undo support func saveContext(completion: (() -> Void)? = nil) { #if os(macOS) if !context.commitEditing() { NSLog("AppDelegate unable to commit editing before saving") } #endif if context.hasChanges { do { try context.save() print("SAVED") completion?() } catch { let nserror = error as NSError #if os(macOS) NSApplication.shared.presentError(nserror) #endif } } } }
2
1
546
Nov ’23
watchOS 10: CloudKit CoreData Sync (NSPersistentCloudKitContainer) Requires Watch on Charger
I've encountered a significant sync issue with watchOS 10 RC on every device combination I've tested, running both iOS 17 and watchOS 10. I'm curious if others have noticed a similar problem. Context: Standalone watchOS app developed in SwiftUI with a companion iOS app. Both apps use NSPersistentCloudKitContainer for bi-directional CloudKit CoreData Sync between the iOS app and watch. Previously, this sync mechanism was near instant in the foreground and took max 1-2 minutes in the background NSPersistentCloudKitContainer has been reliable since the app was first developed in the watchOS 6/iOS 13 era. Issue: In watchOS 10 RC, sync can take hours--and doesn't even occur when the app is in the foreground. Sync only reliably happens when the watch is placed on the charger, seemingly only if the charge is over 50%. Once taken off the charger, the watch will continue push and receive CoreData changes briefly before becoming unresponsive to sync again. Additional Info: The problem persists even when recompiled with the latest Xcode RC. It's problem is consistent in both production and development CloudKit environments. The CloudKit log shows no watch activity upon CoreData object updates, until placed on the charger (with over 50% charge). The sync starts in the background while the watch is charging. The iOS app, however, reflects immediate activity in the CloudKit log after a CoreData change from the device and reacts promptly to pushes when they eventually occur from the watch. The NSPersistentCloudKitContainer code hasn't changed since it was implemented. Background modes for remote notifications are set correctly, and when sync finally happens, it's accurate. I'm stumped. Perhaps there's a new watchOS 10 setting affecting CloudKit sync QoS? Or could this be a known watchOS 10 RC bug?
12
6
1.7k
Nov ’23
CoreData causing error running Mac App - Is there a way to reset app so it starts fresh?
Yesterday I had issues with my CoreData database for my Multiplatform app which resulted in changes to the schema. I reset the CloudKit version and, on the iOS simulator, had to delete my app and run the code again to get it to work. My issue is with the macOS target. I'm getting the same fatalError (Fatal error: Unresolved error Error Domain=NSCocoaErrorDomain Code=134140 "Persistent store migration failed, missing mapping model.") when running the macOS app but I can't figure out how to delete it so I can run it fresh like I did with the iOS version. I've found where the application is created (in the Debug directory ultimately within my app's Build directory) and removed them all but when run the newly created application has the same error. Should I move up the directory structure and remove the entire app directory within Xcode/DerivedData or is there another way? Wanted an answer before I do something I can't go back from :) Thanks, Kyra
2
1
1.8k
Nov ’23
Swift UI Preview for Multiple Entity
Hello, I've created multiple Entity in CoreData, that has a relationship to one another. However, I'm unable to create @discardableResult to use in SwiftUI preview. /// Entity data for use with canvas previews. static var preview: Entity1 {         let entities1 = Entity1.makePreviews(count: 1)         return entities1[0] } @discardableResult static func makePreviews(count: Int) -> [Entity1] {         var contents = [Entity1]()         let viewContext = PersistenceController.preview.container.viewContext         let persistenceController = PersistenceController.shared         for index in 0..<count {             let entities1 = Entity1(context: viewContext)             entities1.id = UUID()             entities1.title = "Amazing day!"             let photo = Photo(context: viewContext)             let imageData = UIImage(named: "Golden Temple")?.jpegData(compressionQuality: 1) ?? Data()             photo.linkedToJournal = journal             let thumbnail = Thumbnail(context: viewContext)             let thumbnailData = persistenceController.thumbnail(with: imageData)?.jpegData(compressionQuality: 1)             thumbnail.data = thumbnailData             thumbnail.photo = photo             let photoDataObject = PhotoData(context: viewContext)             photoDataObject.data = imageData             photoDataObject.photo = photo             contents.append(entities1)         }         return contents } You may also try to use the sample code from https://developer.apple.com/documentation/coredata/sharing_core_data_objects_between_icloud_users to build a SwiftUI Preview. Appreciate if you could suggest how to build a SwiftUI preview as it saves a lot of development effort and time. Thank you very much!
1
0
1k
Nov ’23
SwiftData - different containers in one app
Hello to team, I have a very challenging issue that I'm not able to fix. I have an app that works with SwiftData Model Container that is generated when the app is launched. In the app I also added a Demo Mode area, where the user can load a mock data and simulating app behavior with more data. In order to switch to demo mode, I'm loading new UI layer and loading a new model container, which is configured to work with the same scheme, but isStoredInMemoryOnly = true. Everything works fine. Now, when the user toggles off the demo mode and go back to the main UI, any persistent changes to the modelContext leads to a crash where it says that the create store is different from the open store. Tried almost everything - I'm not sure even if it's possible to have this flow with SwiftData, butI'm trying. Please advice. Thanks a lot!
0
1
584
Nov ’23
iOS NSCoreDataCoreSpotlightDelegate, how to reindex all items
I have implemented NSCoreDataCoreSpotlightDelegate, and I missed out the part where the default expirationDate is 1 month, now items that I have added 3 months ago are not showing up in the search index.How do I get NSCoreDataCoreSpotlightDelegate to reindex all the items?I used to be able to call this:mcdcsd.searchableIndex(CSSearchableIndex.default(), reindexAllSearchableItemsWithAcknowledgementHandler: {})And the first time, it will reindex all the items, but if I re-run the app with the above line uncommented again, it will not reindex.If I uninstall the app, install back, then uncomment the above line, then it will index all again for the first time.How do I get it to reindex everything again?
2
0
1.7k
Nov ’23
Core Data error: 'cannot use an attribute type of "Composite"' when "Used with CloudKit" checked
Hello, I'm running into an odd error. I'm trying to add a composite attribute called period to a Core Data Entity named Report, but I'm getting a compile-time error that points to my xcdatamodel file: Report.period cannot use an attribute type of "Composite". In my model configuration I have "Used with CloudKit" box checked. If I uncheck this box, the error goes away and everything builds fine. But the documentation for NSCompositeAttributeDescription says: You may use composite attributes anywhere you use standard attributes, including lightweight migrations and CloudKit, through NSPersistentCloudKitContainer. So it seems like I should be able to use composite attributes in Core Data with CloudKit syncing enabled. What am I missing here? Is this a bug, or am I doing something wrong?
2
0
344
Oct ’23
CoreData CloudKit sync resulting in main thread initialisation issue
I'm using Xcode 14 beta 4, on macOS Monterey 12.5, developing a Mac app that's using CoreData CloudKit sync — with my context being configured with two configurations (one for a local cache and one data synced via CloudKit). An issue recently appeared where the app crashes soon after start-up with this message: [threadmgrsupport] _TSGetMainThread_block_invoke:Main thread potentially initialized incorrectly, cf <rdar://problem/67741850> And this stack trace: Thread 1 Queue : com.apple.main-thread (serial) #0 0x000000018b86b5e8 in ___NSAssertMainEventQueueIsCurrentEventQueue_block_invoke () #1 0x0000000105b563a8 in _dispatch_client_callout () #2 0x0000000105b58300 in _dispatch_once_callout () #3 0x000000018b86a2dc in _DPSNextEvent () #4 0x000000018b868e14 in -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] () #5 0x000000018b85afe0 in -[NSApplication run] () #6 0x000000018b82c6fc in NSApplicationMain () #7 0x00000001ae281e98 in specialized runApp(_:) () #8 0x00000001aee10588 in runApp<τ_0_0>(_:) () #9 0x00000001ae88626c in static App.main() () Things work again, if I disable the CloudKit sync by not setting cloudKitContainerOptions on my "Cloud" configuration. Similarly, it works on first install, but reappears on subsequent runs. The code is in a package that's shared with an iOS version of the app, which has no such issues. I also tried going back to previous commits that worked before, and it works again until at some point it stops working again for the same commits. So it doesn't seem to be caused directly by my code, but rather by something that the CloudKit sync is doing. Any help would be much appreciated.
3
2
2.1k
Oct ’23
The model configuration used to open the store is incompatible with the one that was used to create the store.
I have deleted the app and try to run the Xcode project again. How can I remove this error ? Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=134020 "The model configuration used to open the store is incompatible with the one that was used to create the store." UserInfo={NSAffectedObjectsErrorKey=<NSManagedObject: 0x6000021c5310>
2
1
1.3k
Oct ’23
iOS 16.4 - UICollectionView and NSFetchedResultsController
Since iOS 16.4, a new exception is raised when "the number of sections and/or items returned by the data source before and/or after performing the batch updates are inconsistent with the updates". This post (iOS 16.4.1 - UICollectionViewController crashes) goes into some more detail, with a response from an Apple engineer, but doesn't explain exactly how this mechanism should/could work with NSFetchedResultsController. When using NSFetchedResultsController, its contents could contain unfetched/faulted core data objects in the tens of thousands. As the NSArray returned by NSFetchedResultsController is a reference type, the developer has no control over the point in time it is updated. Further, there doesn't seem to be a way to copy NSFetchedResultsSectionInfo without triggering a deep copy. Therefore, if a developer wishes to play nicely with the UICollectionView batch updates mechanism they are forced to make a deep copy of the results which means faulting a bunch of potentially unused objects and defeating a primary benefit of NSFetchedResultsController. Is there a more performance optimised way of getting UICollectionView batch updates and NSFetchedResultsController to play nicely?
3
0
965
Oct ’23
new swift concurrency model with core data willSave()
anyone know how to use the new swift concurrency model (ie. await context.perform and context.perform) core data willSave()? for example. how would I ensure this is thread safe? changing self.managedObjectContext!.performAndWait to await self.managedObjectContext!.perform means I have to change willSave() to async which then means it never gets called because its not overriding the correct method anymore. override public func willSave() { self.managedObjectContext!.performAndWait { if self.modifiedDate == nil || Date().timeIntervalSince(self.modifiedDate!) >= 1 { self.modifiedDate = Date() } } super.willSave() }
0
0
577
Oct ’23
CoreData+CloudKit Model Migration Failing
So I've been developing happily with CoreData+CloudKit for a while now, and really haven't run into any issues until this. I've migrated my model a couple of times, and it worked fine, but this was the first time I added a new entity to the model. If I run on a device (any device, iPhone, iPad, Mac via Catalyst), I get a migration error. Here's the entirety of my persistentContainer setup: public var persistentContainer: NSPersistentContainer = { let modelPath = Bundle(for: CoreDataStack.self).url(forResource: "Progress", withExtension: "momd")! let model = NSManagedObjectModel(contentsOf: modelPath)! let container = NSPersistentCloudKitContainer(name: "Progress", managedObjectModel: model) container.loadPersistentStores(completionHandler: { (storeDescription, error) in if let error = error as NSError? { print(storeDescription) fatalError("Unresolved error \(error), \(error.userInfo)") } }) return container }()And here's the error I'm getting:Callstacks=true}}}CoreData: annotation: : Attempting recovery from error encountered during addPersistentStore: Error Domain=NSCocoaErrorDomain Code=134110 "An error occurred during persistent store migration." UserInfo={sourceURL=[…].sqlite, reason=Cannot migrate store in-place: constraint violation during attempted migration, destinationURL=[…].sqlite, NSUnderlyingError=0x600000cebae0 {Error Domain=NSCocoaErrorDomain Code=134111 "(null)" UserInfo={_NSCoreDataOptimisticLockingFailureConflictsKey=( ), NSUnderlyingException=Constraint unique violation, reason=constraint violation during attempted migration, NSExceptionOmitCallstacks=true}}}Important note: I am not using contraints at all in this model. I have created a new Entity, given it a single attribute, and a single one-to-many relationship with an existing entity. That's all.Also, here's an excerpt from the logs when I have -com.apple.CoreData.MigrationDebug enabled, pointing to an issue with the CloudKit metadata tables update step:CoreData: annotation: Completed persistent history metadata tables update CoreData: annotation: Beginning CloudKit metadata tables update CoreData: annotation: Failed lightweight migration on connection CoreData: annotation: Rolling back formal transactionAnyone seen this, have any idea what could be failing, or how to make cloudkit/coredata happy with a migration like this?
10
3
4.7k
Oct ’23
Designing a Word of the Day App: Balancing Local Word Data with Cloud-Based Learning Records"
I have a "Word of the Day" application with a CoreData database containing approximately 14,000 word entities. These entities have attributes like frequency (indicating how often a word appears in some corpus), definition, and pronunciation. Currently, this information is stored locally. I want to add a feature that allows users to store their learning data about these words. For example, users should be able to mark words with a rating of 1, 2, or 3 to indicate their degree of familiarity with the words. My ultimate goal is to display the highest-frequency word with a user familiarity rating of 1 and display it as todays word of the day. The challenge I'm facing is how to set up my app so that learning data is stored in the cloud while the word information remains stored locally. I've seen that you can create different configurations and have one connected to CloudKit, but this approach may make it difficult to write queries that combine information from both the Word entity and the LearningData entity. I'm looking for suggestions on how to address this issue effectively.
0
0
283
Oct ’23
Core data failed to find the migration mapping file due to the derived attribute.
Hi, I have found another bug if I'm not mistaken in core data. When we are using a derived attribute in our xcdatamodel file after making the mapping file there is an error: Can't find mapping model for migration If you are looking to reproduce the bug I made a super simple example app at my branch in the link below: https://github.com/hamed8080/LeitnerBox/tree/fix_migration Keep that in mind if I remove the derived attribute and afterward make a migration with a new mapping file, it works correctly!
1
0
614
Oct ’23
In CloudKit, how do I check whether the user of an app is the share-sender or the share-receiver?
In other words, I’m trying to setup sharing in my app using Core Data with CloudKit. Basically the user can share their list of CloudKit records with other users of the app. Should that other user accept that share from that share-sender, the share-receiver can then sees the sender’s list of records. This part works as I need it to. This is fine. As well, the app is largely being written in SwiftUI. However, what I also need is to give the user the ability to see only their own records, or to set one of their records to private. I think I can figure this logic out, but I’m still not sure of how to filter the sender versus receiver of that share, AKA owner vs participant. I’ve tried several things to figure this out, and I keep coming up empty. I feel like there’s a good way to do this, however what I’ve done always has issues. Can someone please describe the best way to do this?
0
0
408
Oct ’23
Receiving NSPersistentStoreRemoteChange notification when app is closed
I use NSPersistentCloudKitContainer to fetch/sync data across multiple devices with the same iCloud account. /// ... container = NSPersistentCloudKitContainer(name: containerName) let description = container.persistentStoreDescriptions.first description?.setOption( true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey) description?.setOption( true as NSNumber, forKey: NSPersistentHistoryTrackingKey) let viewContext = container.viewContext viewContext.automaticallyMergesChangesFromParent = true viewContext.mergePolicy = NSMergePolicy.mergeByPropertyObjectTrump NotificationCenter.default.addObserver( forName: .NSPersistentStoreRemoteChange, object: container.persistentStoreCoordinator, queue: .main ) { _ in Task { @MainActor [weak self] in // fetch new data and update widgets with it // ... viewContext.fetch ... // WidgetCenter.shared.reloadAllTimelines() } } } /// ... Everything works fine when my app in the foreground. How can I achieve the same when my app is closed/ in the background? I know that CloudKit can send silent push notification which can wake up my app but I've never seen the implementation of it using CoreData NSPersistentCloudKitContainer
0
0
428
Oct ’23
Swift Concurrency, Core Data, "partially thread-safe" types & Sendable conformance
Hi, Xcode warns me that NSPersistentContainer, NSManagedObjectContext, NSPersistentHistoryTransaction and NSPersistentHistoryToken are non-Sendable types and therefore cannot cross actor boundaries. These warnings occur even when I use @preconcurrency import CoreData (only works with Xcode 14.0 Beta, in Xcode 13.4.1, it says '@preconcurrency' attribute on module 'CoreData' is unused) I understand that types in Core Data have yet to be marked as Sendable when it makes sense. Although NSPersistentHistoryTransaction, and NSPersistentHistoryToken are reference types, they should qualify to be marked as Sendable in the future since these are immutable types, am I right? NSPersistentContainer provides variables and methods like viewContext and newBackgroundContext(). It would make sense that this type is thread-safe. However, I'm not sure about it, especially regarding its loadPersistentStores(completionHandler:) method. Is NSPersistentContainer (and its subclass NSPersistentCloudKitContainer) thread-safe and should it be marked as Sendable in the future? The case of and NSManagedObjectContext confuses me the most. Indeed, it has both thread-safe methods like perform(_:) and thread-unsafe methods like save(). Still, it should be possible to cross actor boundaries. Would such a "partially thread-safe" type quality to be marked as Sendable? If not, how can it cross actor boundaries? The reason I'm asking these questions is that I have a CoreDataStack type. It has mutable variables, such as var lastHistoryToken: NSPersistentHistoryToken?, needs to perform work without blocking the main thread, such as writing the token to a file, has async methods, uses notification observers, and needs to be marked as Sendable to be used by other controllers running on different actors. Since this type is related to back-end work, I made it an Actor. This type exposes the persistent container, and a method to create new background threads. However, I need to explicitly annotate all methods using await context.perform { ... } as nonisolated in this actor. Not doing so causes a data race to be detected at runtime. Is this a bug, and is making a Core Data stack an Actor the proper way to do it or is there a better solution? Any help would be greatly appreciated. Thanks. Xcode Configuration Xcode 13.4.1, Xcode 14.0 beta 5 The Xcode project is configured with Other Swift Flags set to -Xfrontend -warn-concurrency -Xfrontend -enable-actor-data-race-checks.
4
2
2.7k
Oct ’23
Fix CloudKit container permissions
I've been using CloudKit for my app recently and I'm trying to add support for a WatchOS app and I've enabled iCloud in capabilities and ticked the container I want to use but I get this error. CoreData: error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate recoverFromPartialError:forStore:inMonitor:]block_invoke(1943): <NSCloudKitMirroringDelegate: 0x282430000>: Found unknown error as part of a partial failure: <CKError 0x28112d500: "Permission Failure" (10/2007); server message = "Invalid bundle ID for container"; uuid = ; container ID = "iCloud.Appname"> I tried creating a new container which worked for both the watch app and iOS app however I would like to use my original container since it has my old data
4
1
5.2k
Oct ’23
Swiftdata: CloudKit integration requires that all attributes be optional, or have a default value set
Unfortunately I can not get swiftdata to work for me. I created a brand new project in Xcode, modified no code and the program fails with this error. Only thing I did was add an iCloud container. The default code that you are given for a swiftadata object is: @Model final class Item { var timestamp: Date init(timestamp: Date) { self.timestamp = timestamp } } Xcode wants that values in this model to ether be optional or have a default value. I thought this was a bug that had been fixed, but I am on the latest Xcode. I am not sure how to proceed. If the default code Xcode gives you won't compile, how am I able to code my own?
1
0
526
Oct ’23