Skip to content
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

Prefer KVO for app storage observation #3487

Merged
merged 3 commits into from
Nov 12, 2024
Merged

Prefer KVO for app storage observation #3487

merged 3 commits into from
Nov 12, 2024

Conversation

stephencelis
Copy link
Member

Observing the firehose notification from Notification Center is not only less performant, but it can cause crashes in SwiftUI if a notification is posted during a view body update, which can occur in certain situations.

Instead, we will prefer KVO unless we detect an invalid key, such as one that contains a . or is prefixed with an @.

Fixes #3311.

}
let willEnterForeground: (any NSObjectProtocol)?
if let willEnterForegroundNotificationName {
willEnterForeground = NotificationCenter.default.addObserver(
forName: willEnterForegroundNotificationName,
object: nil,
queue: nil
queue: .main
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this forces a thread hop, which means we should avoid the crash even on invalid KVO keys, but we could also do an explicit dispatch if we're not confident here.

@@ -60,22 +60,6 @@ extension AppStorageKeyPathKey: PersistenceKey, Hashable {
observer.invalidate()
}
}

private class Observer: NSObject {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This observer wasn't used anymore.

Comment on lines +41 to +45
warm-simulator:
@test "$(PLATFORM_ID)" != "" \
&& xcrun simctl boot $(PLATFORM_ID) \
&& open -a Simulator --args -CurrentDeviceUDID $(PLATFORM_ID) \
|| exit 0
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to see if we can reduce CI flakiness by warming the simulators.

@stephencelis stephencelis merged commit 12268ed into main Nov 12, 2024
13 checks passed
@stephencelis stephencelis deleted the app-storage-fix-2 branch November 12, 2024 00:47
@ddanilyuk
Copy link
Contributor

@stephencelis Looks like this also fixes this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash when using @Shared appStorage during certain conditions
3 participants