How to avoid CoreData corruption when developing Share extension to CoreData in AppGroup?

We plan to develop a Share extension.

The CoreData is storing its SQLite file in AppGroup folder.

The share extension will run in a different process than main app's.

Since share extension and main app are running in different process, both will have their own instance of CoreData. However, even there are multiple instances of CoreData in different processes, their underlying are pointing to a single same SQLite file.

Under https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW1

It mentions

When you set up a shared container, the containing app—and each contained app extension that you allow to participate in data sharing—have read and write access to the shared container. To avoid data corruption, you must synchronize data accesses. Use Core Data, SQLite, or Posix locks to help coordinate data access in a shared container.

But it isn't clear, what are detailed steps required to

  1. How can we synchronize access to CoreData among 2 processes?
  2. Who should responsible to consume relevant store change, and update the single token file? (https://developer.apple.com/documentation/coredata/consuming_relevant_store_changes) Should it be main app, share extension or both?

Thank you.

  • On iOS extension and app may rarely be running at the same time as starting the extension will put the app to background. You can find more info here

Add a Comment