In our application, we store user information (Username, Password, accessToken, Refresh token, etc.) in the keychain. However, after performing a hard reboot (unplugging and plugging back in), when we attempt to retrieve the ‘refresh token’ or ‘access token’ from the keychain, we receive the old token instead of the newly saved one.
Issue with Retrieving Updated Tokens from Keychain After Hard Reboot on Apple TV
Apple TV is a bit weird because a) it’s an iOS-style device, and b) you can yank the power. Normally, iOS-style devices have a battery and, even in the case wehre you run the battery completely down, the system usually has enough power to shut down cleanly.
Given that, I suspect keychain is not doing a sufficiently good job of synching its changes to disk. Under the cover the data protection keychain is implemented as an SQLite database. SQLite use a journal so that, if things go south, it’ll either roll forward or roll back to a consistent state. The most likely cause of the symptoms you’ve described is that it’s chosen to roll back.
IMO we could do a better job with this, especially given the realities of Apple TV. I think this’ll likely end up as a bug report. However, before I send you down that path, I thought I’d check whether you can reproduce this in a small test project?
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
Apple TV is a bit weird because a) it’s an iOS-style device, and b) you can yank the power. Normally, iOS-style devices have a battery and, even in the case wehre you run the battery completely down, the system usually has enough power to shut down cleanly.
Given that, I suspect keychain is not doing a sufficiently good job of synching its changes to disk. Under the cover the data protection keychain is implemented as an SQLite database. SQLite use a journal so that, if things go south, it’ll either roll forward or roll back to a consistent state. The most likely cause of the symptoms you’ve described is that it’s chosen to roll back.
IMO we could do a better job with this, especially given the realities of Apple TV. I think this’ll likely end up as a bug report. However, before I send you down that path, I thought I’d check whether you can reproduce this in a small test project? Such a project might:
-
Read and displays the keychain item an launch.
-
Have a button that sets the keychain item to a string represent of the current date.
You could then launch it, click the button, pull the plug, launch it again, and see what you get.
The benefit of having a small test project like this is that it isolate this keychain problem from all the other code in your main app.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
We created the test application mentioned above, however the issue still exists there as well. OS version - tvOS 17.6
Scenario
-
Open the application and click the "Save Date & Time to Keychain" button to save the current date and time to the Keychain.
-
Wait for 5 to 20 seconds after saving the date and time.
-
Reboot or unplug the Apple TV.
-
Plug the Apple TV back in and open the application again.
Observation
Upon reopening, the application retrieves the previous date and time value instead of the one recently saved to the Keychain.
Please find the Test application
[https://drive.google.com/file/d/17cgqZ0bSJQia5L-o5o6Cw2ZC2ekn4reo/view?usp=drive_link)