AppGroup intermittent permission errors

I recently started saving a file, where I hold some app state, to an AppGroup in order to share it with my widget extension. Of the some 16k daily active users of my app, 55 are initially unable to read the file (it might not yet be created). And they are unable to write a file to the AppGroup as well. I only know this due to logging the error to Crashlytics.

Error Reading: "The file “BFTPreferences” couldn’t be opened because you don’t have permission to view it. Error Code:257"

My App sees this error and informs the user to restart their device. I have not been contacted by any of these users for support, so I assume the restart fixes things.

Has anyone seen this before? Is restarting the device the only fix? Am I doing something wrong?

The only solution I can think of currently is to save the file to the app's Documents directory and save a copy to the AppGroup for use in my extensions. At least then the main app won't have an issue and it will just be the widget unable to display data until a restart.

Reading the file:

do {
            // archive data
            let data = try PropertyListSerialization.data(fromPropertyList: preferences, format: .xml, options: 0)
            // write data
            do {
                if let groupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupIdentifier) {
                    let fullPath = groupURL.appendingPathComponent(preferencesFileName)
                    
                    try data.write(to: fullPath)
                }
                else {
                    fatalError("Unable to find app group \(appGroupIdentifier)")
                }
            }
            catch {
                logthis("Failed to write dictionary data to disk. \(error.localizedDescription)") 
            }
        }
        catch {
            logthis("Failed to archive dictionary. \(error.localizedDescription)")
        }

Writing the file:

 do {
                if let groupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupIdentifier) {
                    let fullPath = groupURL.appendingPathComponent(preferencesFileName)
                    
                    let data = try Data(contentsOf: fullPath)
                    if let dictionary = try PropertyListSerialization.propertyList(from: data, format: nil) as? NSMutableDictionary {
                        preferences = dictionary
                    }
                }
                else {
                    fatalError("Unable to find app group \(appGroupIdentifier)")
                }
            } catch {
                if (error as NSError).code == NSFileReadNoSuchFileError {
                    // file doesn't exist so create it
                }
                else {
                    logthis("Couldn't read BFTPreferences:\(error.localizedDescription)\nError Code:\((error as NSError).code)") <--error code 257 is caught here
                }
            }
AppGroup intermittent permission errors
 
 
Q