-
Notifications
You must be signed in to change notification settings - Fork 120
Core Data: Prevent crash on the Blaze Dashboard when saving fails #15546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Core Data: Prevent crash on the Blaze Dashboard when saving fails #15546
Conversation
|
|
jaclync
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the change is to post a new notification that only one class subscribes, I think the changes are pretty confined and we can just smoke test the app to ensure the Blaze dashboard card works as before without crashes. Left some comments.
| do { | ||
| try persistentContainer.persistentStoreCoordinator.destroyPersistentStore(at: storeURL, type: .sqlite, options: nil) | ||
| NotificationCenter.default.post(name: .StorageManagerDidResetStorage, object: self) | ||
| NotificationCenter.default.post(name: .StorageManagerDidInvalidateCachedData, object: self) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: what's the part that invalidates cached data? maybe it could more specific to what this function does like StorageManagerDidDropDatabase/StorageManagerDidDropStorage?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in e7ce752.
| // Listens when the core data stack is rest. | ||
| NotificationCenter.default.addObserver(self, selector: #selector(handleStorageDidReset), name: .StorageManagerDidResetStorage, object: nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓ Maybe this observation of StorageManagerDidResetStorage can still be kept, since this notification is still posted after destroying the database in createPersistentContainer's recovery upon error. Or do we also want to replace StorageManagerDidResetStorage with the new notification there for consistency? I'm not sure if StorageManagerDidResetStorage notification has always been posted in createPersistentContainer from git as there was some refactoring on the file.
woocommerce-ios/Storage/Storage/CoreData/CoreDataManager.swift
Lines 187 to 190 in 68f756a
| try container.persistentStoreCoordinator.destroyPersistentStore(at: storeURL, | |
| ofType: storeDescription.type, | |
| options: nil) | |
| NotificationCenter.default.post(name: .StorageManagerDidResetStorage, object: self) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point! This has been restored in 7109840.
| refetchAllResultsControllers() | ||
| } | ||
|
|
||
| /// Refetching all the results controllers is necessary after a storage reset in `onDidResetContent` | ||
| /// callback and before reloading UI that involves more than one results controller. | ||
| func refetchAllResultsControllers() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💬 How about trying the new notification fix first, to isolate the issue? If the crash still happens with a different stack trace, we know it's a different issue and can consider adding the changes to refetch both results controllers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good! I revert the change in 361f079.
…oard when there's database reset" This reverts commit 5821693.
|
Thanks @jaclync for the reviews! I addressed the suggestions in the subsequent commits. This PR is ready for another look. |
jaclync
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Thanks for making these changes and we can observe the crash after the release.
| @objc func handleStorageDidReset() { | ||
| @objc func handleStorageDidInvalidateData() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: how about marking resetTimestampsValues as @objc, removing this function, and calling resetTimestampsValues from the two notifications instead? I wasn't very sure the "invalidate data" part in the function name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea! Done in 56c7182.
| } | ||
| } | ||
|
|
||
| func test_core_data_database_drop_clears_timestamps_stores() throws { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for adding this test case!

Closes WOOMOB-316
Description
In #14961 I added a workaround to drop the database and force crash the app to help users recover from their corrupted database. This caused an unexpected crash on the Blaze dashboard upon resetting the database, causing the app crash earlier without details about the saving failure.
This PR attempts to prevent this by:
SessionManagerto clear the timestamps for cached data.- Adding a future-proof update to prevent crash on the Blaze dashboard when the database is reset.Steps to reproduce
Smoke test the app with a store that's eligible for Blaze and confirm that the Blaze dashboard is still loaded without issues.
Testing information
Screenshots
N/A
RELEASE-NOTES.txtif necessary.Reviewer (or Author, in the case of optional code reviews):
Please make sure these conditions are met before approving the PR, or request changes if the PR needs improvement: