Our team has recently added support to our app for Live Activities where the source of the data is driven from the app itself (not push notifications).
We've noticed a crash happening in our core data code caused by the following error thrown by the addPersistentStore function where it would attempt to recover and eventually crash. Here's an error we created to help us debug that contains the error details:
Domain: CoreData Code: 1 NSLocalizedDescription: Error performing migration for databaseName=mydb.sqlite. Error details=The file couldn’t be saved because you don’t have permission. - userinfo: ["reason": No permissions to create file; code = 1]
After some trouble shooting, we managed to reproduce the issue by doing a hard reboot while we're running a live activity. It appears that when the device starts back up, the Live Activity starts which triggers the app to hit didFinishLaunchingWithOptions which is where we get our Core Data store initialized.
The problem is that our app uses Data Protection using NSFileProtectionCompleteUntilFirstUserAuthentication and we'd prefer to keep it that way.
The Core Data db is present in the app sandbox and we're also seeing logs to suggest a failure trying to access NSUserDefaults as well.
Is there an accepted solution for this? Is it expected that a Live Activity would cause an application to launch prior to the device being unlocked for the first time? Is there a way to change that?
I'm afraid my response is a bit late here...
Are there other scenarios that may exist where our app could be launched without being user-initiated?
Yes.
Over time we've added more and more APIs that can launch an app into the background. Prewarming also means that an app can end up being woken in the background without previously entering the foreground. At this point, I don't think an app can really assume it will "only" be launched into the foreground, nor do I think trying to predict and individually "handle" all of the background possibilities is really practical. The better approach is to be prepared that it might happen and adjust your apps behavior accordingly.
Due to our usage of the Data protection access level NSFileProtectionCompleteUntilFirstUserAuthentication any access to our entire app sandbox is inaccessible and forces the application to exhibit untestable and bad user experiences
First off, make sure that this is REALLY the issue. It may very well be the case here (I havne't checked), but I've had seen many cases where a developer thought the issue was caused by the device protection state (like running prior to first unlock) when the actual issue was that some data was being stored at the wrong protection level (complete).
However, if you're app cannot operate prior to first unlock and you're being launched prior to first unlock, then you only have two options:
- Block your apps "normal" operation until the device unlocks and you're able to access your data.
- Call exit() (or abort) to prevent further problems/harm.
In either case, you'd also want to "tell" the user what's going on (update your activity UI, post a notification, etc.) so that they can take action to resolve the issue.
__
Kevin Elliott
DTS Engineer, CoreOS/Hardware