I developed core data based app and implemented iCloud sync feature after it was introduced in iOS 13.
I enabled iCloud kit, used NSPersistentCloudKitContainer
instead of NSPersistentContainer
and added several lines of code to sync core data with iCloud.
Sync works fine. The problem is that when I uninstall app and reinstall app, it doesn't fetch iCloud data at first time. I have to restart app or open another screens to let Core Data to be synced with iCloud. Is there anyway I can check if core data is being synced with iCloud or wait until it finishes syncing?
Thanks.
Use Core Data with CloudKit to give users seamless access to the data in your app across all their devices. Core Data with CloudKit combines the benefits of local persistence with cloud backup and distribution. Core Data provides powerful object graph management features for developing an app with structured data.
Open Xcode and create a new iOS project based on the Single View App template. Name the app HitList and make sure Use Core Data is checked. Checking the Use Core Data box will cause Xcode to generate boilerplate code for what's known as an NSPersistentContainer in AppDelegate. swift.
When Apple introduced changes to Core Data + CloudKit integration in 2019, they sold developers on a dead-simple API: add iCloud sync to your Core Data app with “as little as one line of code.” That one line, of course, is simply changing NSPersistentContainer to NSPersistentCloudKitContainer and enabling a few capabilities in the project settings.
With just a couple of line of code you can implement Core Data in SwiftUI to manage your data locally and synchronisation to the cloud, so that you can use your app across all the platforms you choose to develop an app (which is much easier to do now thanks to SwiftUI).
Syncing data between iOS devices has become a simple process thanks to iCloud. Many iOS apps use iCloud to store data. This makes their information easier to synchronize across your devices when you install the same app. You might wonder which of your apps use iCloud syncing. You can easily see the list on your device.
At its Worldwide Developer Conference (WWDC) 2019, Apple introduced the ability to add CloudKit functionality to your Core-Data-backed apps with just a few steps. In theory, you needed only three steps.
There is no API exposed to get the completion handler on sync of all CKRecord's to NSManagedObject's.
Also the update is working using a background silent notification so we can expect any change anytime. So its good to not having completion handler
You can create one attribute to check the sync time, just like a date when last sync happened to local record
and check the lastModificationDate
(By default CKRecord has this property) of the same record in iCloud.
So everytime when you launch the app check for all the records manually if any update is required, then wait for notification of update and keep on checking the sync status
Note: Be careful this may lead to delay your launch process if from any other device user is very active and new records are getting inserted or modified.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With