Core Data not returning results in ShieldConfiguration Extension, but works fine in other extensions

Hi everyone,

I’m using Core Data in several extensions (DeviceActivityMonitor, ShieldAction, and ShieldConfiguration). It works perfectly in DeviceActivityMonitor and ShieldAction. I’m able to successfully fetch data and log the correct count using a fetch request.

However, when I try the same setup in the ShieldConfiguration extension, the fetch request always returns 0 results. The CoreData and App Group setup appears to be correct since the first two extensions fetch the expected data.

I’ve also previously tested storing the CoreData objects separately in a JSON-FIle using FileManager and it worked without issues—though I’d prefer not to handle manual encoding/decoding if possible.

The documentation mentions that the extension runs in a sandbox, restricting network requests or moving sensitive content. But shouldn’t reading data (from a shared App Group, for instance) still be possible within the sandbox, as it is the case with the Files, what is the difference there? In my case, I only need to read the data, as modifications can be handled via ShieldActionExtension.

Any help would be greatly appreciated!

I haven't tried with ShieldConfiguration, but one thing may be worth mentioning is Data Protection Classes. As an example, if the data protection class of the Core Data store is set to .complete, the store won't be accessible for a process that runs in the background.

Other than that, I don't have a strong reason to believe that ShieldConfiguration should be special regarding sharing Core Data store via an App Group container. If fetching from your main app gets the data and fetching from your ShieldConfiguration extension doesn't, I'd check if the store URL is exactly the same. The store URL should be at persistentcontainer.persistentStoreDescriptions.first?.url (see NSPersistentStoreDescription.url).

"But shouldn’t reading data (from a shared App Group, for instance) still be possible within the sandbox, as it is the case with the Files, what is the difference there?"

Yes, if your extension can access a file in an App Group container, it should be able to access a Core Data store at the same place.

If you see otherwise, and don't mind to provide a sample project to demonstrate the issue, I would take a closer look.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Thanks for your answer @DTS Engineer,

I've created a minimum reproducable example project. Here is the GitHub Repo.

Keep in mind that you need to test this on a real device, since it wouldn't block the apps on the simulator and therefore won't show the ShieldConfiguration.

Thanks in advance,

Maxi

Core Data not returning results in ShieldConfiguration Extension, but works fine in other extensions
 
 
Q