13

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.

Ioan Moldovan
  • 2,272
  • 2
  • 29
  • 54
  • I delayed displaying the main view from SceneDelegate and icloud data showed up. definitely would love an answer on how to wait for icloud sync – BokuWaTaka Jun 03 '20 at 05:21
  • 1
    Just be aware that iCloud has been experiencing various issues since a couple days ago. This could be it. – rs7 Jun 03 '20 at 21:27
  • I do it the same as @BokuWaTaka. Is there any way to monitor the syncing status? – Muz Sep 11 '20 at 07:47
  • 2
    In iOS **14** there is a new event you can listen to: `NSPersistentCloudKitContainer.eventChangedNotification`, how to use it is described in this answer: [link](https://stackoverflow.com/questions/59138880/nspersistentcloudkitcontainer-how-to-check-if-data-is-synced-to-cloudkit/63927190#63927190) – Falkone Nov 30 '20 at 10:44

3 Answers3

5

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.

Saranjith
  • 11,242
  • 5
  • 69
  • 122
  • When the data is synced, there is logs. Is there any way to read the log for checking the status? – Muz Sep 11 '20 at 07:51
0

Currently, there is no API provided for iCloud sync completion. What you can do is

  • "this will buy you enough time" is not going to be a good solution here, as unfortunately slow network connections and other interruptions will make this solution unreliable – professormeowingtons Nov 23 '20 at 07:16
0

I have to restart app or open another screens to let Core Data to be synced with iCloud.

To solve this I'm using a notification observer for NSManagedObjectContextObjectsDidChange event.

When reinstalled app is first opened, it shows empty screen and after some time, when data is synced, the notification is triggered and the page is reloaded with the data.

However I also couldn't find any way to wait and show loading message while data is being synced.

Akmal
  • 7
  • 4