0

I'm trying to connect my app to CloudKit to save a copy of the user's data online. However when we test with a new install, I want the app to wait until it has downloaded all necessary data from the cloud before deciding which screen to show next.

Is there a way of knowing when CloudKit has finished downloading. I can see in the output that an event occurs:

CoreData: CloudKit: CoreData+CloudKit: -[NSCloudKitMirroringDelegate checkAndExecuteNextRequest]_block_invoke(3225): <NSCloudKitMirroringDelegate: 0x600003324a80>: No more requests to execute.

Can I intercept this?

theDuncs
  • 4,649
  • 4
  • 39
  • 63

1 Answers1

0

You can subscribe to changes in the Core Data context via NotificationCenter (NSManagedObjectContext.didMergeChangesObjectIDsNotification), or subscribe to changes from a NSFetchedResultsController to detect when your data has populated.

However the main trick is knowing if your data store is actually empty when the app launches, or if it is just waiting to sync. Since there's no API to notify state changes of Core Data syncing with CloudKit, you will have to use another mechanism to decide if you should treat it as an empty/new database or wait for data to sync.

This article (not mine) has suggestions on how to handle this scenario.

tl;dr: Set a flag in CloudKit to indicate your DB is initialized by creating a CKRecord, then fetch this record on launch to determine if you should wait to sync or start fresh.

ewerx
  • 403
  • 4
  • 10