I’m trying to access a shared store on an app I’m building on Xcode 15.4 with a WidgetKit extension and it runs perfectly fine on the simulator but as soon as I archive it and install it on my actual iPhone for testing purposes, it crashes. The crash log pointed to a function I wrote, specifically getting the URL for the shared storage on the device. It only returns nil.
This is said function
public extension URL {
/// Returns a URL for the given app group and database pointing to the sqlite database.
static func storeURL(databaseName: String) -> URL? {
guard let fileContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.groupname.shared") else {
fatalError("Shared file container could not be created.")
}
return fileContainer.appendingPathComponent("(databaseName)")
}
}
Code that handles loading the shared store:
lazy var container: NSPersistentContainer = {
let storeURL = URL.storeURL(databaseName: "StoredData")
let storeDescription = NSPersistentStoreDescription(url: storeURL!)
let container = NSPersistentContainer(name: "StoredData")
container.persistentStoreDescriptions = [storeDescription]
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// fatalError("Unresolved error (error), (error.userInfo)")
Alert(title: Text("Unable to load items"), message: Text("(error.userInfo)"))
}
})
return container
}()
I’ve got no idea what to do about this. I’ve verified that the app group name isn’t spelt incorrectly (which should have been obvious to me since it works on the simulator) but still no luck
4
Did you tried adding .sqlite
to the databaseName: "StoredData"
?
Try this extension:
/// Returns a URL for the given app group and database pointing to the sqlite database.
/// - Parameters:
/// - appGroup: Appgroup string value
/// - databaseName: What will be the db name
/// - Returns: URL for the shared file
static func storeURL(forAppGroup appGroup: String, withDBName databaseName: String) -> URL? {
guard let fileContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup) else {
return nil
}
return fileContainer.appendingPathComponent("(databaseName).sqlite")
}
2