Push to CloudKit from Share Extension

Hi All


I am developing a CoreData + CloudKit App for iOS, based partly on the code in CoreDataCloudKitDemo.


The intention of the App is that content will be used in the App but added from a Share Extension.

The CoreData Store is configured to reside in a shared App Group.


Addition and editing of content is functioning but I find that when new content is saved to CoreData from the Share Extension, data does not get pushed to CloudKit until the App itself is brought to the foreground. Which clearly is not the requirement.


Saves made within the App itself eg. from an edit, are pushed to CloudKit immediately.


How do I ensure this happens from the Share Extension?

OK, I have the solution, posting it incase anyone else has the same problem.


The entitlements file for the Share Extension was missing some keys that existed in the App :


I needed to add values for iCloud Container Identifiers and iCloud Services

I no longer find this solution works reliably.


Sending a DarwinNotification from the App Extension to the App may be making it more reliable.


If it really needs this Notification, it would make me think of two issues :

  1. CloudKit synchronisation is not functioning properly when data is saved to CoreData in a shared App Group by an App Extension.
  2. For correct synchronisation to occur after a save from an App Extension, the containing App would have to be running.


The real problem is reliability.


Sometimes, the save does propogate to other devices in both cases (parent app running or not), sometimes it does not.

Sometimes the propogation is partial (missing relationships).


Should I expect CoreData + CloudKIT as it stands to be production-ready or am I doing something wrong?

For iCloud Container Identifiers you have the name of your database and for iCloud Services just cloudKit? I have both of these set and have the same issue that you originally posted about, not reliable – sometimes it works, sometimes it doesn't. Curious if you have made any additional discoveries? Super frustrating!

I shared some thoughts on this a month ago in another answer.

In summary:

  • Saving to Core Data itself is reliable from an extension
  • The extension likely is killed before the sync to CloudKit is scheduled/complete.

This is why you see the behavior of needing to launch your app for syncing to occur.

There is no workaround at this point, as the syncing is done in their internal API, but I'm hoping my conversation with the Apple engineer was enough for them to consider improvements this year.

Push to CloudKit from Share Extension
 
 
Q