已配置App Group仍无法从App Extension访问Core Data的问题求助
Hey there! Let's tackle your Core Data sharing issue—those SQLite errors are definitely frustrating. After looking through your code, I noticed a couple of key issues that are likely causing the problem:
1. Mismatched App Group Identifiers
First off, your main app and extension are using different App Group IDs!
In your main app's persistent container code, you're using:
let storeURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.io.project.ios.core.data")!.appendingPathComponent("CallerData.sqlite")
But in your extension's code, you've got:
let storeURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.io.project.core.data")!.appendingPathComponent("CallerData.sqlite")
See the difference? The extension's ID is missing the ios segment. That means your extension is looking for the SQLite file in a completely different shared container than your main app is writing to.
Fix: Update the extension's App Group ID to match the main app exactly. Also double-check that both targets in Xcode's Capabilities > App Groups have the same group selected (no typos!).
2. Overcomplicated Extension Persistent Container
Your extension's persistent container includes migration logic that's probably unnecessary right now, and might be introducing conflicts. Since the extension only needs to read the data (Call Directory extensions are typically read-only for this use case), we can simplify the setup to focus on accessing the shared store directly.
Replace your extension's persistent container code with this simplified version:
lazy var persistentContainer: NSPersistentContainer = { let container = NSPersistentContainer(name: "CallerData") // Use the SAME App Group ID as your main app guard let appGroupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.io.project.ios.core.data") else { fatalError("Failed to retrieve App Group container URL—check your group ID!") } let storeURL = appGroupURL.appendingPathComponent("CallerData.sqlite") // Set up the store description to point directly to the shared location let storeDescription = NSPersistentStoreDescription(url: storeURL) container.persistentStoreDescriptions = [storeDescription] container.loadPersistentStores(completionHandler: { (storeDescription, error) in if let error = error as NSError? { fatalError("Unresolved Core Data error: \(error), \(error.userInfo)") } }) return container }()
3. Verify Core Data Model Target Membership
Make sure your CallerData.xcdatamodeld file is included in both targets. To check this:
- Select the model file in Xcode
- Open the File Inspector (right sidebar)
- Under Target Membership, ensure both your main app and Call Directory Extension are checked.
If the extension doesn't have access to the model, it won't recognize the Caller entity, which can also lead to file access errors.
4. Clean & Rebuild
After making these changes:
- Clean your project (
Cmd+Shift+K) - Delete the app from your test device/simulator
- Rebuild and run the main app first to populate the shared store
- Then trigger the extension reload to test if it can read the data
These steps should resolve the "Couldn’t be opened" error—most likely the App Group ID mismatch was the root cause. Let me know if you still run into issues!
内容的提问来源于stack exchange,提问作者1978273884

