I have been using NSPersistentCloudKitContainer in my app for a while now. My main issue at the moment is that even though I have followed all of the documented steps for setting up background silent notifications, they don't seem to be working. (I query for the Core Data state from a Shortcut/Intent and can verify that the Core Data store hasn't been updated.) This hasn't been too much of an issue, but with new system features like widgets (WidgetKit), this is an issue I can no longer ignore. I'm not sure if it's completely out of my control (e.g. the OS just has decided not to deliver the notifications in the background) or if there's some undocumented step I need to take to ensure it is set up correctly. Has anyone gotten background syncing to work properly with NSPersistentCloudKitContainer? Did you have to do anything special to get it to work? Any tips for debugging what might be going wrong?
Search results for
NSPersistentCloudKitContainer
589 results found
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I have a Core Data + Couldkit App where any changes made on other device is not synched. I had to call the fetch method again refresh the data. I even added the necessary lines, let container = NSPersistentCloudKitContainer(name: myappcontainername) container.viewContext.automaticallyMergesChangesFromParent = true The following are the CoreData logs after making a change, CoreData+CloudKit: -[NSCloudKitMirroringDelegate finishedAutomatedRequestWithResult:](2119): Finished request '<NSCloudKitMirroringExportRequest: 0x6> -UUID' with result: <NSCloudKitMirroringResult: 0x> success: 1 madeChanges: 1 error: (null) What else should I be doing to make this work?
Hi there! I built an app using Core Data and CloudKit using NSPersistentCloudKitContainer - https://developer.apple.com/documentation/coredata/nspersistentcloudkitcontainer. However, now that I'm looking to add support for extensions, I need to migrate the existing store from its default location using migratePersistentStore(_:to:options:withType:) - https://developer.apple.com/documentation/coredata/nspersistentstorecoordinator/1468927-migratepersistentstore so it can be accessed from a new App Group. Once I update the app to the new build with the migration logic, the store appears empty to CloudKit and the existing objects are re-downloaded to the device. This effectively duplicates all of the records. It seems as though the migrated records are new NSManagedObjects, which would explain why CloudKit doesn't have a corresponding CKRecord. Upon detecting that the CloudKit version of the object does not exist on device, NSPersistentCloudKitContainer ensures that it is created (appea
This happens automatically when you use NSPersistentCloudKitContainer. See https://developer.apple.com/videos/play/wwdc2019/202/?time=1291 at time 21:30.
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags:
I've seen that the documentation of NSPersistentCloudKitContainer has been updated after the WWDC with two new Type Properties: class let eventChangedNotification: NSNotification.Name and class let eventNotificationUserInfoKey: String I think that are related to your question but I haven't tried them. I'm looking for this feature from the last year and I hope that in iOS 14 it will be implemented.
Topic:
App & System Services
SubTopic:
iCloud & Data
Tags:
My two cents... My Core Data implementation for SwiftUI using the new App and Scene protocols and Window container! Credit to: mtsrodrigues for how to deal with change of Scene in his answer in this thread. KrakenDev's article on how to write a one line singleton My SwiftUI App... import SwiftUI @main struct YourApp: App { @Environment(.scenePhase) private var scenePhase @StateObject private var persistentStore = PersistentStore.shared var body: some Scene { WindowGroup { ContentView() .environment(.managedObjectContext, persistentStore.context) } .onChange(of: scenePhase) { phase in switch phase { case .active: print((#function) REPORTS - App change of scenePhase to ACTIVE) case .inactive: print((#function) REPORTS - App change of scenePhase to INACTIVE) case .background: print((#function) REPORTS - App change of scenePhase to BACKGROUND) savePersistentStore() @unknown default: fatalError((#function) REPORTS - fatal error in switch statement for .onChange modifier) } } } func savePersistentStore() { persiste
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags:
So for any passers-by that are interested in how I eventually achieved this... (Credit to mtsrodrigues for how to deal with change of Scene in his answer in this thread... https://developer.apple.com/forums/thread/650876) My Core Data implementation for SwiftUI and the new App and Scene protocols and Window container! My SwiftUI App... struct MyApp: App { @Environment(.scenePhase) private var scenePhase @StateObject private var persistentStore = PersistentStore.shared var body: some Scene { WindowGroup { ContentView() .environment(.managedObjectContext, persistentStore.context) } .onChange(of: scenePhase) { phase in switch phase { case .active: print((#function) REPORTS - App change of scenePhase to ACTIVE) case .inactive: print((#function) REPORTS - App change of scenePhase to INACTIVE) case .background: print((#function) REPORTS - App change of scenePhase to BACKGROUND) savePersistentStore() @unknown default: fatalError((#function) REPORTS - fatal error in switch statement for .onChange modifier) } } } func
Topic:
UI Frameworks
SubTopic:
General
Tags:
Hi, I am trying to fetch CKShare for NSManagedObject. Firstly I get CKRecord for it using NSPersistentCloudKitContainer's record(for:) method. And getting situation when: First time record(for:) gives correct CKRecord I successfully create CKShare, see it in online Dashboard Next time record(for:) gives old CKRecord which don't have share yet, it's outdated! I swipe down notification Notification Center or minimise the app (go to home screen), and when go back, database is being immediately updated and record(for:) returns correct CKRecord with existing share now. I can get correct CKRecord if I use only recordID from CKRecord that I get from record(for:), and fetch updated CKRecord using fetch(withRecordID:completionHandler:). But it increase awaiting time twice because I need to get response for correct CKRecord and then fetch CKShare for it. If no share for CKRecord, I could get that nil immediately but now I have to fetch correct updated CKRecord. So, anyone know how to keep local database update
Hi, I've set up NSPersistentCloudKitContainer with mirroring and it works well. I also managed how to create share for records. But how I can get shared record synced with my Core Data? Example: User A (owner) shared record with user B, how that record (with 1-to-many relationship) can be synchronised with user B Core Data?
I'm in the middle of updating my app to only support iOS 14+ and am moving to the new @main app structure. I'm trying to figure out how to deal with code in my App and Scene Delegate: mainly code related to Core Data and CloudKit persistent container. Stuff like lazy var persistentContainer: NSPersistentCloudKitContainer = {...} and let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy context.automaticallyMergesChangesFromParent = true Where do I put this kind of stuff in the new @main world?
I was able to fix this by moving my data store into its own class and using the same thing for both the app and previews: import CloudKit import CoreData import Foundation class DataStore { static let shared = DataStore() lazy var persistentContainer: NSPersistentContainer = { let container = NSPersistentCloudKitContainer(name: Structure) container.loadPersistentStores(completionHandler: { _, error in if let error = error { fatalError(Unresolved error (error)) } let context = container.viewContext context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy context.automaticallyMergesChangesFromParent = true // other setup here }) return container }() func saveContext() { let context = persistentContainer.viewContext if context.hasChanges { do { try context.save() } catch { fatalError(Unresolved error (error)) } } } }
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags:
Hi, I wrote an app that helps me count how many grams of protein I eat each day. I'm the only user now, and the app uses Core Data and the CloudKit container, to sync data across my devices. I was thinking about releasing it in App Store, and I noticed this in the app review guidelines: may not store personal health information in iCloud. Does that mean we cannot use Core Data with NSPersistentCloudKitContainer for an app like this? Thanks.
Hi, I am developing an app with CoreData + CloudKit and wanted my Widget to fetch data from CloudKit to display. Everything is working on iOS / watchOS, but on WidgetKit the result from my fetch is always empty. I already configured the CloudKit capability on the Widget extension, then I am creating a new NSPersistentCloudKitContainer the and on public func timeline(with context: Context, completion: @escaping (Timeline<Entry>) -> ()) { I am trying to let cardsFetch = NSFetchRequest<Card>(entityName: Card) do { let cards = try persistentContainer.viewContext.fetch(cardsFetch) the result is always 0 and the same fetch on iOS returns the data. Is this supposed to work?
The documentation - https://developer.apple.com/documentation/coredata/mirroring_a_core_data_store_with_cloudkit/creating_a_core_data_model_for_cloudkit for Creating a Core Data Model for CloudKit states that Unique constraints aren’t supported. but I've tried to set one up and it seems to be working fine (locally, I haven't tried syncing yet) on Xcode 11.5 & iOS 13.5. If I'm not mistaken, when NSPersistentCloudKitContainer was first introduced Xcode displayed an error if you tried to add a constraint to an Entity, but that doesn't seem to happen anymore. Has this restriction been lifted or do I just remember it incorrectly and it never showed any errors? If the restriction has not been lifted, does anyone know how to make sure that Entities are not duplicated? It'll be easy to add a cleanup operation after every sync, but so far I am not aware of any way to know when the sync has fully finished - https://developer.apple.com/forums/thread/652607. Thanks.
In development I've switched to NSPersistentCloudKitContainer and have begun the terrifying process of trying to test as many scenarios as possible to make sure nobody loses any data. During some testing I saw an error like this: I was able to fix this, but it was only affecting some scenarios and if this had happened to a real user they (and I) would never have known about the problem, and their data would not have synced to CloudKit. If the store fails to load you get an error, but if it loads but doesn't sync you don't. So my question is: is there a way to receive these kind of errors in code? I have looked for notifications and delegate methods and I can't find any.