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

Clarify SwiftUI ViewModel state null semantics #268

Merged

Conversation

darronschall
Copy link
Contributor

@darronschall darronschall commented Mar 16, 2024

This simplifies code and cleans up call-site use, and makes it explicit when to use each helper. When your Kotlin ViewModel StateFlow has non-optional values, use the state helper in SwiftUI. When your StateFlow has an optional type and might have null values, use stateNullable in SwiftUI.

Note: There isn't compiler help when deciding between state and stateNullable in SwiftUI. Choosing state where stateNullable is required will cause a runtime crash due to force unwrapping. Take care to use the correct helper.

This PR also introduces state and stateNullable helpers for KotlinBase. When Kotlin/Native exports to Objective-C, all custom classes share a common KotlinBase interface (which implements NSObject and has isEqual available).

This simplification makes the library slightly easier to use and perhaps a little more intuitive. Instead of forcing callers through state(_:equals:mapper:) for custom types and making them define their own equals and mapper, it's now possible to just use the simpler state(_) helper.

This applies the same change made to `state` in icerockdev#209 to `stateNullable` to prevent potential memory churn.
This simplifies code and cleans up call-site use, and makes it explicit when to use each helper. When your Kotlin ViewModel `StateFlow` has non-optional values, use the `state` helper in SwiftUI. When your `StateFlow` has an optional type and might have `null` values, use `stateNullable` in SwiftUI.

Note: There isn't compiler help when deciding between `state` and `stateNullable` in SwiftUI. Choosing `state` where `stateNullable` is required will cause a runtime crash due to force unwrapping. Take care to use the correct helper.
@darronschall darronschall changed the base branch from master to develop March 16, 2024 12:08
When Kotlin/Native exports to Objective-C, all custom classes share a common `KotlinBase` interface (which implements `NSObject` and has `isEqual` available).

We take advantage of this in order to add `state` and `stateNullable` helpers without requiring downstream users to define their own `equals` and `mappers`.

This simplification makes the library slightly easier to use and perhaps a little more intuitive. Instead of forcing callers through `state(_:equals:mapper:)` for custom types, it's possible to just use the simpler `state(_)` helper.
@Alex009 Alex009 added this to the 0.17.0 milestone Mar 24, 2024
@Alex009 Alex009 merged commit bbc5714 into icerockdev:develop Mar 24, 2024
3 checks passed
@darronschall darronschall deleted the clarify-swiftui-state-null-semantics branch March 24, 2024 16:48
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.

2 participants