[tvOS] Fix First Login Crash #1481
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Resolves: #1477
tvOS seems to crash on first login due to a memory allocation issue. By wrapping these operations in
DispatchQueue.main.async
, we ensure that:UI Updates Happen on the Main Thread: SwiftUI requires all UI updates to happen on the main thread. When we modify Defaults or post notifications that trigger UI updates, doing this on a background thread can cause crashes.
Thread Safety for Shared Resources:
Container.shared.currentUserSession.reset()
deals with a shared resource that might be accessed from multiple threads. The async wrapper ensures thread-safe access.Memory Access Synchronization: The crash was an
EXC_BAD_ACCESS
error, which often happens when memory is deallocated on one thread while being accessed on another. The async wrapper ensures proper synchronization.Deferred Execution: By deferring these operations to the next run loop cycle, we give any pending UI updates a chance to complete before changing application state.
(According to Claude) tvOS is particularly sensitive to threading issues because:
Does it make sense to, where possible, move these operations to the
viewModel
instead?