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

SwiftUI CoreData relationship binding Bug
I developing simple app to calculate personal money income and consumption. I use CORE DATA to save my model. The models consists of this CDCatalogWallet var name: String var ref: String CDCatalogStateIncome var name: String var ref: String CDDocumentIncome var ref: String var wallet: CDCatalogWallet (relationship has one) var states_: CDDocumentIncomeState (relationship has many) CDDocumentIncomeState var cdCatalogStateIncome: CDCatalogStateIncome (relationship has one) var document: CDDocumentIncome (relationship has one) CDDocumentIncome hasMany -> CDDocumentIncomeState hasOne -> CDCatalogStateIncome The bug shows when we pushing CDDocumentIncomeState view The model is simple. The CDDocumentIncome has relationship property cdCatalogStateIncome (relationship many). We open view with CDDocumentIncome and want to add new cdCatalogStateIncome to CDDocumentIncome Then we open view with new cdCatalogStateIncome. On this screen cdCatalogStateIncome has a relationship to CDDocumentIncomeState. But view does not opened. Console constantly print messages DocumentIncomeStateDetailView: @self changed. DocumentIncomeStateDetailView: @self changed. DocumentIncomeStateDetailView: @self changed. DocumentIncomeStateDetailView: @self changed. Also memory increase constantly and very fast. I understand that the view renders constantly. But I cannot figure out what cause a bug. The full code is available on github Github Project
1
0
281
Feb ’24
SwiftUI View Processing on CoreData deleteObject
How can one avoid SwiftUI processing of views referencing a CoreData entity that has been deleted? I've taken to 'wrapping' View body's with the following: struct ManagedObjectDeleteWrapper<Object, Root> : View where Object : NSManagedObject, Root : View { var object: Object var root: (Object) -> Root var body: some View { Group { if object.isFault { EmptyView() } else { root (object) } } } } like such: struct GameView: View { @EnvironmentObject private var game: Game var body: some View { ManagedObjectDeleteWrapper (object: game) { _ in // Show `game` attributes and relationships } } } but this has become quite tedious. It is not as simple a wrapping the 'top-level' view (in my App, three or so TabViews), but one must wrap multiple subviews if they also reference game. How can I approach this short of using game.isFault or ManagedObjectDeleteWrapper everywhere? I'm cognizant that perhaps my 'view flow' is leading to this problem; is there a generic approach to view hierarchies that avoids the problem? Maybe 'only delete an object from a list view; only show the object content view a navigation link on that list view' ? My current design has three tab views showing game content and one tab showing 'competitions' (as set of games); a game is deleted from the competitions tab... and if that game is being displayed all the other tab views are invalid.
0
0
444
Feb ’24
Has anyone successfully used NSStagedMigrationManager?
I've been trying to build an example of NSStagedMigrationManager from some Core Data migration tests to replace a custom migration manager solution I'd constructed, without much success. The Core Data model has seven model versions. Most support lightweight migration, but two of the migrations in the middle of the sequence used NSMappingModel. In the first beta, just attempting to construct an NSStagedMigrationManager from the series of stages failed with an unrecognized selector. That no longer happens in b4, but I now get an error that "Duplicate version checksums across stages detected." If I restrict myself to just the first three versions of the model (that only require lightweight migration), I can build the migration manager. But if I attempt to use it to migrate a persistent store, it fails somewhere in NSPersistentStoreCoordinator with a nilError. The documentation is almost nonexistent for this process, and the WWDC session that introduced it isn't much more than a breezy overview. So maybe I'm holding it wrong? (And, yes: FB12339663)
5
0
951
Feb ’24
How to loop and place map markers with MKAnnotationView
From core data I generate data for map marker pins. When user selects a marker pin I display the data in a popup label with multiple rows, when I click on the pin. This is working for one marker pin. Now, I need to iterate over a list and generate several of those markers each with unique popup label data. The code: struct MapView: UIViewRepresentable { var annotationOnTap: (_ title: String) -> Void @Binding var pins: [GpsData.MyEndPt] let key: String private static var mapViewStore = [String : MKMapView]() func makeUIView(context: Context) -> MKMapView { if let mapView = MapView.mapViewStore[key] { mapView.delegate = context.coordinator return mapView } let mapView = MKMapView(frame: .zero) mapView.delegate = context.coordinator MapView.mapViewStore[key] = mapView return mapView } func updateUIView(_ uiView: MKMapView, context: Context) { uiView.addAnnotations(pins) } func makeCoordinator() -> MapCoordinator { MapCoordinator(self) } final class MapCoordinator: NSObject, MKMapViewDelegate { var parent: MapView init(_ parent: MapView) { self.parent = parent } func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { let PINID:String = "MyEndPoint" var mPin: MKMarkerAnnotationView? = nil let subtitleView = UILabel() subtitleView.font = subtitleView.font.withSize(12) subtitleView.numberOfLines = 0 if (mPin == nil ) { mPin = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: PINID) mPin?.canShowCallout = true } else{ mPin?.annotation = annotation } mPin?.leftCalloutAccessoryView = nil let btn = UIButton(type: .detailDisclosure) mPin?.rightCalloutAccessoryView = btn let zip:String = "77065" let formattedSalesStr:String = "100" let totalTargetText:String = "500" let paddedLoad = formattedSalesStr.padding(toLength: 50, withPad: " ", startingAt: 0) let paddedCapacity = totalTargetText.padding(toLength: 50, withPad: " ", startingAt: 0) subtitleView.text = "Total sales: " subtitleView.text! += paddedLoad subtitleView.text! += " \r\n" subtitleView.text! += "Total Target: " subtitleView.text! += paddedCapacity subtitleView.text! += " \r\n" subtitleView.text! += paddedzip mPin!.detailCalloutAccessoryView = subtitleView return mPin! } } } As you can see in the above method I have hardcoded values for my popup marker label. So I tried to iterate over the @Binding pins, populate each mPin, and return an array of MKAnnotationView, like this: func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> [MKAnnotationView] { let PINID:String = "MyEndPoint" var i:Int = 1 var mPins = [MKAnnotationView]() for detail in self.parent.pins { var mPin: MKMarkerAnnotationView? = nil let subtitleView = UILabel() subtitleView.font = subtitleView.font.withSize(12) subtitleView.numberOfLines = 0 if (mPin == nil ) { mPin = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: PINID) mPin?.canShowCallout = true } else{ mPin?.annotation = annotation } mPin?.leftCalloutAccessoryView = nil let btn = UIButton(type: .detailDisclosure) mPin?.rightCalloutAccessoryView = btn subtitleView.text! += String(i) subtitleView.text = "Total Load: " subtitleView.text! += String(detail.sales) subtitleView.text! += " \r\n" subtitleView.text! += "Total Target: " subtitleView.text! += String(detail.target) subtitleView.text! += " \r\n" i += 1 // } mPin!.detailCalloutAccessoryView = subtitleView mPins.append(mPin!) } return mPins } The marker pins show up, but then the label does not popup. How to fix this?
0
0
375
Feb ’24
Fatal crash when using CoreData and deleting item from list with Section headers
Greetings - The following code appears to work, but when a list item is deleted from a Category section that contains other list items, the app crashes (error = "Thread 1: EXC_BREAKPOINT (code=1, subcode=0x180965354)"). I've confirmed that the intended item is deleted from the appData.items array - the crash appears to happen right after the item is deleted. I suspect that the problem somehow involves the fact that the AppData groupedByCategory dictionary and sortedByCategory array are computed properties and perhaps not updating as intended when an item is deleted? Or maybe the ContentView doesn't know they've been updated? My attempt to solve this by adding "appData.objectWillChange.send()" has not been successful, nor has my online search for solutions to this problem. I'm hoping someone here will either know what's happening or know I could look for additional solutions to try. My apologies for all of the code - I wanted to include the three files most likely to be the source of the problem. Length restrictions prevent me from including the "AddNewView" code and some other details, but just say the word if that detail would be helpful. Many, many thanks for any help anyone can provide! @main struct DeletionCrashApp: App { let persistenceController = PersistenceController.shared // Not sure where I should perform this command @StateObject var appData = AppData(viewContext: PersistenceController.shared.container.viewContext) var body: some Scene { WindowGroup { ContentView() .environment(\.managedObjectContext, persistenceController.container.viewContext) .environmentObject(appData) } } } import Foundation import SwiftUI import CoreData class AppData: NSObject, ObservableObject { // MARK: - Properties @Published var items: [Item] = [] private var fetchedResultsController: NSFetchedResultsController<Item> private (set) var viewContext: NSManagedObjectContext // viewContext can be read but not set from outside this class // Create a dictionary based upon the category var groupedByCategory: [String: [Item]] { Dictionary(grouping: items.sorted(), by: {$0.category}) } // Sort the category-based dictionary alphabetically var sortedByCategoryHeaders: [String] { groupedByCategory.map({ $0.key }).sorted(by: {$0 < $1}) } // MARK: - Methods func deleteItem(itemObjectID: NSManagedObjectID) { do { guard let itemToDelete = try viewContext.existingObject(with: itemObjectID) as? Item else { return // exit the code without continuing or throwing an error } viewContext.delete(itemToDelete) } catch { print("Problem in the first do-catch code: \(error)") } do { try viewContext.save() } catch { print("Failure to save context: \(error)") } } // MARK: - Life Cycle init(viewContext: NSManagedObjectContext) { self.viewContext = viewContext let request = NSFetchRequest<Item>(entityName: "ItemEntity") request.sortDescriptors = [NSSortDescriptor(keyPath: \Item.name, ascending: true)] fetchedResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: viewContext, sectionNameKeyPath: nil, cacheName: nil) super.init() fetchedResultsController.delegate = self do { try fetchedResultsController.performFetch() guard let items = fetchedResultsController.fetchedObjects else { return } self.items = items } catch { print("failed to fetch items: \(error)") } } // end of init() } // End of AppData extension AppData: NSFetchedResultsControllerDelegate { func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) { guard let items = controller.fetchedObjects as? [Item] else { return } self.items = items } } import SwiftUI import CoreData struct ContentView: View { @Environment(\.managedObjectContext) private var viewContext @EnvironmentObject var appData: AppData @State private var showingAddNewView = false @State private var itemToDelete: Item? @State private var itemToDeleteObjectID: NSManagedObjectID? var body: some View { NavigationView { List { ForEach(appData.sortedByCategoryHeaders, id: \.self) { categoryHeader in Section(header: Text(categoryHeader)) { ForEach(appData.groupedByCategory[categoryHeader] ?? []) { item in Text(item.name) .swipeActions(allowsFullSwipe: false) { Button(role: .destructive) { self.itemToDelete = appData.items.first(where: {$0.id == item.id}) self.itemToDeleteObjectID = itemToDelete!.objectID appData.deleteItem(itemObjectID: itemToDeleteObjectID!) // appData.objectWillChange.send() <- does NOT fix the fatal crash } label: { Label("Delete", systemImage: "trash.fill") } } // End of .swipeActions() } // End of ForEach(appData.groupedByReplacementCategory[categoryHeader] } // End of Section(header: Text(categoryHeader) } // End of ForEach(appData.sortedByCategoryHeaders, id: \.self) } // End of List .navigationBarTitle("", displayMode: .inline) .navigationBarItems( trailing: Button(action: { self.showingAddNewView = true }) { Image(systemName: "plus") } ) .sheet(isPresented: $showingAddNewView) { // show AddNewView here AddNewView(name: "") } } // End of NavigationView } // End of body } // End of ContentView extension Item { @nonobjc public class func fetchRequest() -> NSFetchRequest<Item> { return NSFetchRequest<Item>(entityName: "ItemEntity") } @NSManaged public var category: String @NSManaged public var id: UUID @NSManaged public var name: String } extension Item : Identifiable { }
3
1
967
Feb ’24
Crash in CoreData (_PFExternalReferenceData)
I'm currently facing an interesting issue. A customer is reporting back that my app is crashing on launch for them. I can see the crash logs (using AppCenter for crash management), and the reason the app is crashing seems to be in CoreData: (Attaching text for seachability, and screenshot for readability): libsystem_platform.dylib _platform_memmove$VARIANT$Haswell CoreData -[_PFExternalReferenceData initForExternalLocation:safeguardLocation:data:protectionLevel:] CoreData -[NSSQLSavePlan _populateRow:fromObject:timestamp:inserted:shouldAddToRowCache:] CoreData -[NSSQLSavePlan _createRowsForSave] CoreData -[NSSQLSaveChangesRequestContext executePrologue] CoreData -[NSSQLCore dispatchRequest:withRetries:] CoreData -[NSSQLCore executeRequest:withContext:error:] CoreData -[NSPersistentStoreCoordinator executeRequest:withContext:error:] CoreData -[NSPersistentStoreCoordinator _routeHeavyweightBlock:] CoreData -[NSPersistentStoreCoordinator executeRequest:withContext:error:] CoreData -[NSManagedObjectContext save:] I have never seen that crash before, it's the first time it is appearing since CoreData has been incorporated into the app (in 2016 or so). Any hints on what could possibly be happening? I am unable to provoke the crash on my end, thus debugging is quite interesting.
1
0
266
Feb ’24
SwiftUI/CoreData: Simultaneous accesses to 0x7f92efc61cb8, but modification requires exclusive access
I'm working on an iOS app using SwiftUI and CoreData and am running into a problem that I cannot seem to figure out. To provide a little bit of information of what I am trying to do: I have two CoreData entities that have a One-To-Many relationship: Tarantula - A tarantula may molt many times (To Many) and when deleted, all of the molts shold also be removed (Cascade) Molt - A molt belongs to a single Tarantula (To One) and when deleted, should have the reference removed from the Tarantula (Nullify) I then have a view that lists all of the molts for a given Tarantula that allows adding and deleting molts. It looks like this: struct MoltListView: View { &#9;&#9;private static let DATE_FORMATTER: DateFormatter = { &#9;&#9;&#9;&#9;&#9;&#9;let d = DateFormatter() &#9;&#9;&#9;&#9;&#9;&#9;d.dateFormat = "MMM d, y" &#9;&#9;&#9;&#9;&#9;&#9;return d &#9;&#9;&#9;&#9;}() &#9;&#9; &#9;&#9;@Environment(\.managedObjectContext) private var viewContext &#9;&#9; &#9;&#9;@ObservedObject private var tarantula: Tarantula &#9;&#9;@FetchRequest private var molts: FetchedResults<Molt> &#9;&#9;@State private var userMessage: String = "" &#9;&#9;@State private var displayMessage: Bool = false &#9;&#9; &#9;&#9;init(tarantula: Tarantula) { &#9;&#9;&#9;&#9;self.tarantula = tarantula &#9;&#9;&#9;&#9;self._molts = FetchRequest(entity: Molt.entity(), &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; sortDescriptors: [NSSortDescriptor(keyPath: \Molt.date, ascending: false)], &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; predicate: NSPredicate(format: "tarantula = %@", tarantula)) &#9;&#9;} &#9;&#9; &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;List { &#9;&#9;&#9;&#9;&#9;&#9;Section(header: Text("Summary")) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text("\(molts.count) Molt\(molts.count == 1 ? "" : "s")") &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;Section(header: Text("Molts")) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;NavigationLink(destination: MoltView(tarantula: tarantula, molt: Molt.newModel())) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text("Add Molt").foregroundColor(.blue) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;ForEach(molts, id: \.self) { molt in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;NavigationLink(destination: MoltView(tarantula: tarantula, molt: molt)) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;Text(MoltListView.DATE_FORMATTER.string(from: molt.modelDate)) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.onDelete(perform: deleteItems) &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;}.alert(isPresented: $displayMessage) { &#9;&#9;&#9;&#9;&#9;&#9;Alert(title: Text("Save Failure"), message: Text(userMessage), dismissButton: .default(Text("Ok"))) &#9;&#9;&#9;&#9;} &#9;&#9;} &#9;&#9; &#9;&#9;private func deleteItems(offsets: IndexSet) { &#9;&#9;&#9;&#9;withAnimation { &#9;&#9;&#9;&#9;&#9;&#9;offsets.map { molts[$0] }.forEach(viewContext.delete) &#9;&#9;&#9;&#9;&#9;&#9;do { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;try viewContext.save() &#9;&#9;&#9;&#9;&#9;&#9;} catch { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;viewContext.rollback() &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;userMessage = "\(error): \(error.localizedDescription)" &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;displayMessage.toggle() &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;} &#9;&#9;} } The error I am experiencing comes from whenever I try to delete a molt from the list view. The app instantly crashes and the error is: Simultaneous accesses to 0x7f92efc61cb8, but modification requires exclusive access Find the complete error here: error - https://developer.apple.com/forums/content/attachment/6d74dcde-d82b-4024-ade0-5936d8926488 I have tried removing the animation block and have played around with removing different UI components/restructuring. The only way I have been able to prevent this error is to remove the delete rule on the Molt->Tarantula relationship from Nullify to No Action. However, this seems more like a hack to me instead of a fix. Was hoping for some help on this issue.
7
0
3.6k
Feb ’24
CoreData with cloudkit sync - partially broken.
When I add or delete data of my entity type I can see the updates (between the simulator and the phone) but when i edit I dont see the updates although table view is reloaded. The console says Ignoring remote change notification because it didn't change any entities tracked by persistent history When I rebuild the app for both device and simulator I see it reflecting the most current changes. Any help? Neerav
0
0
251
Feb ’24
SwiftUI Lag on Core Data/SwiftData Updates During Background Tasks
Hello, developers, I'm experiencing UI lag in SwiftUI when updating Core Data or SwiftData entities in the background, noticeable during scrolling. This issue arises regardless of whether the updated entity is currently displayed. For instance, updating a "Song" entity with a new localURL from a background download and saving background context affects an unrelated "Artists" list view. Scenario: The lag is most evident when completing background downloads and updating entity properties, causing a hitch in scrolling interactions when using SwiftUI @FetchRequest or @Query for displaying data. Observations: The issue occurs with both Core Data @FetchRequest and SwiftData @Query property wrappers. It affects unrelated views, suggesting a broader issue with SwiftUI's handling of data updates during user interactions. Seeking Community Insights: Has anyone faced similar issues with SwiftUI and background data updates? Any strategies for reducing this lag/hitch? Insights into SwiftUI's data update and view re-rendering process in this context? For a practical example, Apple's project on loading and displaying a large data feed demonstrates this issue. Try tapping the refresh button while scrolling to observe the lag. Appreciate any advice or solutions! Best Regards, [Personal Information Edited by Moderator]
1
0
381
Feb ’24
CoreData Transformable Types and SecureCoding
I have a CoreData entity with a transformable property data that stores an NSDictionary. All of the classes inside the dictionary conform to NSSecureCoding I have been getting many instances of this warning : 'NSKeyedUnarchiveFromData' should not be used to for un-archiving and will be removed in a future release In trying to clean up the warning and future-proof my app, but I am running into a lot of trouble. My understanding is that using NSKeyedUnarchiveFromData as the transformer in my data properties attribute should work, since the top level class is a dictionary and all of the contents conform to NSSecureCoding. However, my data dictionary is now coming back as NSConcreteMutableData so I cannot access the data in it. I have also tried creating a custom value transformer with additional allowedTopLevelClasses, but that hasn't helped, and again, the topLevel type is an NSDictionary, which should be allowed. Thank you for any guidance.
3
0
430
Feb ’24
App unresponsive when calling record(for:) on NSPersistentCloudKitContainer
I'm experiencing an unresponsive UI since MacOS 14.0 and iOS 17.0 when calling record(for: ) or recordID(for:) on the instance of NSPersistentCloudKitContainer. On MacOS, the UI freeze almost always happens when calling the function. On iOS, it is necessary that the device (or simulator) does not have any network connection. I would like to ask if anyone experienced the same problem. I have posted the problem twice to Apple via the Feedback app (once for iOS and once for MacOS). No reply yet on MacOS but on iOS Apple marked it as resolved because apparently no one but me has experienced this problem. In the meantime, I have set up a minimum reproducible example app (MRE).: https://github.com/DominikButz/NotesApp-Cloud-Kit-Record-UI-Freeze- Anyone interested, please read the readme of the repository. It includes step by step instructions on how to reproduce the bug. I can't rule out I have misunderstood the usage of CoreData and CloudKit - in that case please point me in the right direction. The app I'm working on should also work offline (and on MacOS!) but it doesn't do so properly as long as this bug exists. I'm exploring switching to SwiftData (which would mean no one using macOS 13 / iOS 16 can use my app...) but I would still need to access cloud kit records even with SwiftData and I fear the bug also exists in SwiftData. Thanks
3
0
303
Feb ’24
coredata entities from public configuration are not synchronized to cloudkit
In Core data public configuration, added new attribute to entities, new entities, but the changes are neither synchronized nor data is transferred to existing container schema in cloudkit. private var _publicPersistentStore: NSPersistentStore? var publicPersistentStore: NSPersistentStore { return _publicPersistentStore! } private var _privatePersistentStore: NSPersistentStore? var privatePersistentStore: NSPersistentStore { return _privatePersistentStore! } private var _sharedPersistentStore: NSPersistentStore? var sharedPersistentStore: NSPersistentStore { return _sharedPersistentStore! } static let shared = PersistenceController() static var preview: PersistenceController = { let result = PersistenceController(inMemory: true) let viewContext = result.container.viewContext do { try viewContext.save() } catch { // Replace this implementation with code to handle the error appropriately. // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. let nsError = error as NSError fatalError("Unresolved error \(nsError), \(nsError.userInfo)") } return result }() let container: NSPersistentCloudKitContainer init(inMemory: Bool = false) { container = NSPersistentCloudKitContainer(name: “GS”) if inMemory { container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null") } guard let defaultDescription = container.persistentStoreDescriptions.first else { fatalError("###\(#function): failed to retrieve a persistent store description.") } let containerIdentifier = defaultDescription.cloudKitContainerOptions!.containerIdentifier print(containerIdentifier) print(defaultDescription.url as Any) let url = defaultDescription.url?.deletingLastPathComponent() print(url as Any) // Public let publicDescription = NSPersistentStoreDescription(url: url!.appendingPathComponent("public.sqlite")) publicDescription.configuration = "Public" print(publicDescription.url) let publicOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: containerIdentifier) publicOptions.databaseScope = .public publicDescription.cloudKitContainerOptions = publicOptions publicDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) publicDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey) // Private let privateDescription = NSPersistentStoreDescription(url: url!.appendingPathComponent("private.sqlite")) privateDescription.configuration = "Private" print(privateDescription.url) let privateOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: containerIdentifier) privateOptions.databaseScope = .private privateDescription.cloudKitContainerOptions = privateOptions privateDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) privateDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey) // Shared guard let sharedDescription = privateDescription.copy() as? NSPersistentStoreDescription else { fatalError("#\(#function): Copying the private store description returned an unexpected value.") } sharedDescription.url = url!.appendingPathComponent("shared.sqlite") print(sharedDescription.url) sharedDescription.configuration = "Shared" let sharedOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: containerIdentifier) sharedOptions.databaseScope = .shared sharedDescription.cloudKitContainerOptions = sharedOptions sharedDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) sharedDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey) container.persistentStoreDescriptions = [publicDescription, privateDescription, sharedDescription] container.loadPersistentStores(completionHandler: { (storeDescription, error) in if let error = error as NSError? { // Replace this implementation with code to handle the error appropriately. // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. /* Typical reasons for an error here include: * The parent directory does not exist, cannot be created, or disallows writing. * The persistent store is not accessible, due to permissions or data protection when the device is locked. * The device is out of space. * The store could not be migrated to the current model version. Check the error message to determine what the actual problem was. */ fatalError("Unresolved error \(error), \(error.userInfo)") } else { if let cloudKitContainerOptions = storeDescription.cloudKitContainerOptions { if #available(iOS 16.0, *) { if .public == storeDescription.cloudKitContainerOptions?.databaseScope { print("loaded public store") // self._publicPersistentStore = container.persistentStoreCoordinator.persistentStore(for: storeDescription.url!) } else if .private == storeDescription.cloudKitContainerOptions?.databaseScope { print("loaded private store") //self._privatePersistentStore = container.persistentStoreCoordinator.persistentStore(for: storeDescription.url!) } else if .shared == storeDescription.cloudKitContainerOptions?.databaseScope { print("loaded shared store") //self._sharedPersistentStore = container.persistentStoreCoordinator.persistentStore(for: storeDescription.url!) } } else { // Fallback on earlier versions } } } }) container.viewContext.automaticallyMergesChangesFromParent = true container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy // external changes trumping in-memory changes. } func save() { let context = container.viewContext if context.hasChanges { do { try context.save() } catch { // Show some error here print("save error") } } } } Tried new container on cloudkit, problem persists. Working previously until I updated Xcode to 15.2 and iOs 16.2. Can you please tell me why coredata is not synchronized for public configuration.
2
0
308
Feb ’24
Core data heavy weight migration issues with big sized data
We are facing a challenging memory issue during a Core Data heavy weight migration in our app. Background: We store sizable data(~250kb/record) in a Core Data entity called "Email." We have roughly 20k records. We are loading the "Email" list using fetchedResultsController with batching. Recently, we split the another entity named "Profile" into "Profile" and "Address" entities, requiring a heavyweight migration. The Problem: This migration consumes an exorbitant amount of memory, causing the OS to kill our app. Interestingly, the "Email" entity is causing the brunt of the memory usage, even though the actual migration code deals with the "Profile" entity to create "Address." If we remove the "sizable" data from "Email" and attempt for migration then the migration is successful. My Questions: Is this memory consumption expected behavior? Why does the "Email" entity, seemingly unrelated to the actual migration, seem to be the main memory culprit? How can I prevent the OS from terminating my app during migration? Are there strategies to optimize memory usage or mitigate memory pressure? Also we are encountering a delay in loading the email list on the very first launch of app. Subsequent launches work comparatively faster. Is that related to storing bigger size data in Email entity?
1
1
295
Feb ’24
Core data issues with refactoring an entity
We have an entity called "EMail" with attributes such as ID, From, To, Subject, Body. Due to various reasons including performance for retrieval, impact on migration etc, we want to refactor this entity into two entities with a parent child relationship: "EMail": ID, From, To, Subject and "EMailBody": ID, Body. Can this be done with an existing model? If so, will this still fall under lightweight migration?
0
0
280
Feb ’24
Core Data Migration
Hello, I’ve been struggling with Core Data lightweight migration since the iOS 17 launch. I have a NSPersistentStoreCoordinator which I use the following options in it: [NSMigratePersistentStoresAutomaticallyOption: true as AnyObject, NSInferMappingModelAutomaticallyOption: true as AnyObject, NSSQLitePragmasOption: ["journal_mode": "DELETE"] as AnyObject] I don’t create new versions when I change my .xcdatamodel. However, lightweight migration worked normally until iOS 17. And there is something else, the migration doesn’t fail for all users. So, if I update my xcdatamodel from version 1 to 2, some users get the following error: An error occurred during persistent store migration., reason: Failed to open the store, underlyingReason: The model used to open the store is incompatible with the one used to create the store Last but not least, my NSPersistentStoreCoordinator’s MOM merges two other models. But these two other models did not have any changes since I started getting this issue. I don’t think the issue is because we are adding new entities or properties in our xcdatamodel because if it were, it would be described in the error we are printing when trying to add the persistent store. I wonder if something has changed in iOS 17 that is causing this issue, which was working normally before. Also, what do you think I should do to fix this issue? Versioning my xcdatamodel? Although I don’t see it as a requirement in Apple’s docs. Removing the two other models from NSPersistentStoreCoordinator? Something else?
1
2
304
Feb ’24
SwiftData + CloudKit process for deduplication / consuming relevant store changes?
It is often the case that offline devices can add duplicate entities that needs to be merged when CloudKit syncs. Consider user-created tags. A user might create a Note, and then tag it with a newly created tag “Family.” On a separate offline device, they might create another note, and create another tag also called ”Family.” On device sync, both duplicate ”Family” tags would need to be identified as duplicates based on their name property, merged to a single entity, and their original relationships consolidated to the single merged Tag. And this needs to happen before the CloudKit sync data is presented to the UI in the main context. With Core Data we have the mechanism to consume relevant store changes described here. These tools allow us to listen for remote change, then process them appropriately (e.g. remove / merge duplicates) and then merge the changes into the app’s main context. This perfectly solves the problem described in the first paragraph above. Apple provides code using this mechanism for deduplicating tags in a sample app. Is there a mechanism to solve this deduplication problem using SwiftData technology without implementing and maintaining a parallel Core Data stack?
2
2
614
Feb ’24
SQlite FTS Queries Running Slow iOS 17.2.1 >=
As the title says, our app has begun running into a strange issue every time a user upgrades their iPhone to the latest iOS versions. It started in 17.2.1 and looks like it made a resurgence in 17.3. Post update the user's FullText Searches against the local sqlite db run egregiously slow. Fully re-installing the app and re-downloading all content seems to correct the issue, after which their searches come back in less than 500 ms tops. When the bug occurs searches can take upwards of 15 secs in some cases to complete... What makes no sense is why all of a sudden this is happening in 17.2.1 >= Our app uses an old school approach (historic code we never removed) to create a local sqlite database from scratch on first app install in the user's device contentDir. this .db file is instantiated on each app load using the following: private func sqliteOpenDB(url: URL) -> OpaquePointer? { var db: OpaquePointer? if sqlite3_open_v2(url.path, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX, nil) == SQLITE_OK { LoggingUtil.log(msg: "\(ContentFilestore.TAG): database file opened successfully") // You must enable foreign key constraints every time the db connects otherwise they're off by default and cascade deletes/fk's won't work sqlite3_exec(db, "PRAGMA foreign_keys = ON;", nil, nil, nil) } else { LoggingUtil.log(msg: "\(ContentFilestore.TAG) Error: unabled to open the database file") } return db; } Prior to iOA 17.2.1 we've never had an issue with this when a user's device upgraded the OS. Now this process continues to work without throwing an error or warning of any kind, but their FTS search db slows to a crawl. It also seems to be completely isolated to the FTS virtual table as well. The actual tables run just fine. My question is did something major change in iOS 17.2.1 >= that I'm missing that would be causing virtual tables to lose their indexing or have some kind of issue that would cause this problem?
3
0
318
Jan ’24
Sharing CoreData objet with nested data
Is there a way to share CoreData data containing complex relationships easily by file? And also to create them easily after receiving the file? The application runs on a secure iPad without iCloud, so CloudKit is not an option. I'd really like to keep the file principle, so that data can be shared via Airdrop, e-mail, file transfer, etc. The ideal would be : data = myNSManagedObject.toJSON() file = createFile(data) share file receive file data = file.data.decodeJSON() myNSManagedObjectContext.add(data) All this without having to browse all the objects to create them individually Thx
0
0
200
Jan ’24