-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Load User Defaults list asynchronously to improve performance #22535
Load User Defaults list asynchronously to improve performance #22535
Conversation
Generated by 🚫 Danger |
@@ -16,40 +16,51 @@ final class BooleanUserDefaultsDebugViewModel: ObservableObject { | |||
} | |||
} | |||
|
|||
@Published var userDefaultsSections: Sections = [] | |||
@MainActor @Published private(set) var userDefaultsSections: Sections = [] |
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.
Decorating this property with @MainActor
means that it must be mutated on the main thread.
|
||
init(coreDataStack: CoreDataStack = ContextManager.shared, | ||
init(coreDataStack: CoreDataStackSwift = ContextManager.shared, |
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.
The CoreDataStackSwift
is the same as CoreDataStack
protocol, but with additional async
APIs.
Task { | ||
await self.load() | ||
} | ||
} |
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.
This load
method is for convenience only so that callers don't have to do the Task { }
wrapping.
let (key, value) = keyValue | ||
result.append(processSingleUserDefault(key: key, value: value)) | ||
private func processGroupedUserDefaults(_ userDefaults: [String: Bool]) async -> [Row] { | ||
let rows = try? await self.coreDataStack.performAndSave { [weak self] context -> [Row] in |
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.
When retrieving Core Data objects from a background thread, it's important not to use the mainContext
since it's meant for the main thread only. Calling the main context from a background thread could cause the app to crash.
Instead, the method coreDataStack.performAndSave
generates a new context and executes out the block on a background thread. This context is then used to fetch Core Data objects.
📲 You can test the changes from this Pull Request in WordPress Alpha by scanning the QR code below to install the corresponding build.
|
📲 You can test the changes from this Pull Request in Jetpack Alpha by scanning the QR code below to install the corresponding build.
|
…issue/22479-debug-screen-local-flags-async-load
e0523c2
to
a4e6276
Compare
Generated by 🚫 dangerJS |
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.
Thank you for the enhancement, Salim! Tested it and it works as expected - there are no freezes when entering the debug screen.
Related PR
This PR depends on:
Description
There was a short ( but noticeable ) delay when navigating to User Default Debug screen, this was due to running the
load
method on the main thread. This PR fixes the issue by running it asynchronously.CleanShot.2024-02-02.at.19.43.29.mp4
CleanShot.2024-02-02.at.19.44.52.mp4
Test Instructions
Regression Notes
Potential unintended areas of impact
Double check the User Defaults can be loaded.
What I did to test those areas of impact (or what existing automated tests I relied on)
N/A
What automated tests I added (or what prevented me from doing so)
N/A
PR submission checklist:
RELEASE-NOTES.txt
if necessary.