Skip to content

Update performance article #1642

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

Merged
merged 2 commits into from
Nov 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -486,14 +486,14 @@ incurring unnecessary costs for sending actions.
In very large SwiftUI applications you may experience degraded compiler performance causing long
compile times, and possibly even compiler failures due to "complex expressions." The
``WithViewStore`` helpers that come with the library can exacerbate that problem for very complex
views. If you are running into issues using ``WithViewStore`` you can make a small change to your
view to use an `@ObservedObject` directly.
views. If you are running into issues using ``WithViewStore``, there are two options for fixing
the problem.

For example, if your view looks like this:

```swift
struct FeatureView: View {
let store: Store<FeatureState, FeatureAction>
let store: StoreOf<Feature>

var body: some View {
WithViewStore(self.store, observe: { $0 }) { viewStore in
Expand All @@ -503,16 +503,25 @@ struct FeatureView: View {
}
```

...and you start running into compiler troubles, then you can refactor to the following:
…and you start running into compiler troubles, then you can explicitly specify the type of the
view store in the closure:

```swift
WithViewStore(self.store, observe: { $0 }) { (viewStore: ViewStoreOf<Feature>) in
// A large, complex view inside here...
}
```

Or you can refactor the view to use an `@ObservedObject`:

```swift
struct FeatureView: View {
let store: Store<FeatureState, FeatureAction>
@ObservedObject var viewStore: ViewStore<FeatureState, FeatureAction>
let store: StoreOf<Feature>
@ObservedObject var viewStore: ViewStoreOf<Feature>

init(store: Store<FeatureState, FeatureAction>) {
init(store: StoreOf<Feature>) {
self.store = store
self.viewStore = ViewStore(self.store)
self.viewStore = ViewStore(self.store, observe: { $0 })
}

var body: some View {
Expand All @@ -521,4 +530,4 @@ struct FeatureView: View {
}
```

That should greatly improve the compiler's ability to type-check your view.
Both of these options should greatly improve the compiler's ability to type-check your view.