Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait until cloud kit data is being synced with core data in swift in iOS13

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.

like image 201
Svetoslav Atanasov Avatar asked Mar 30 '20 10:03

Svetoslav Atanasov


People also ask

Should I host core data in CloudKit?

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.

How do you implement core data in iOS Swift?

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.

How do I integrate iCloud sync with core data + CloudKit?

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.

Why implement core data in SwiftUI?

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).

How does iCloud syncing work between iOS devices?

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.

How many steps does it take to add Cloudkit to core-data?

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.


1 Answers

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

What's next

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.

like image 88
Saranjith Avatar answered Oct 10 '22 17:10

Saranjith