-
Notifications
You must be signed in to change notification settings - Fork 9
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
Fix profile access on ProfileStore #1384
Conversation
@@ -23,16 +23,11 @@ final actor ProfileStore { | |||
} | |||
|
|||
extension ProfileStore { | |||
func profile() async -> Profile { | |||
try! await profileSubject.first() |
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 implementation would crash the app if parent task was cancelled, as here a force unwrap would be done when a CancellationError is thrown. Instead, profileSubject
is transformed to AsyncCurrentValueSubject<Profile?>
to be able to access the current profile in non-async context.
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.
btw I wonder if our init should be updated, we do:
private init() {
Task {
for try await state in await ProfileStateChangeEventPublisher.shared.eventStream() {
if case let .loaded(profile) = state {
self.profileSubject.send(profile)
}
self.profileStateSubject.send(state)
}
}
}
I would think we would wanna do 2 changes:
- Assign the task to a variable
- use
[weak self]
to not have retain cycle
I think 1. is not "needed" because we have a retain cycle, which keeps the Task around,
I think 2. is not "needed" because this is essentially a singleton we need for the duration of the app life time.
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.
I didn't do this in the first place exactly for the reasons you describe, as we don't gain anything. Storing in variable might be slightly confusing, as one would do that, if they want to potentially cancel the task at some point.
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
Fixed the profile access on Profile store to avoid crashing the app if some parent task was cancelled.