Skip to content

Commit f93688e

Browse files
committed
add overview
1 parent fb49650 commit f93688e

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Guide.docc/Articles/MakeInitializerLowCost.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,32 @@
22
Viewのイニシャライザは何度も呼び出されるため、低コストに保ちます。
33

44
## Overview
5+
高コストなイニシャライザはよくあるパフォーマンス低下の原因です。
6+
7+
```swift
8+
struct DogRootView: View {
9+
@State private var model = FetchModel()
10+
11+
var body: some View {
12+
DogList(model.dogs)
13+
}
14+
}
15+
16+
@Observable class FetchModel {
17+
var dogs: [Dog]
18+
19+
init() {
20+
fetchDogs()
21+
}
22+
23+
func fetchDogs() {
24+
// Takes a long time
25+
}
26+
}
27+
```
28+
29+
この例では`DogRootView`のイニシャライザで`FetchModel`がイニシャライズされます。
30+
`FetchModel`のイニシャライザで処理に時間のかかる`fetchDogs`が同期的に呼び出されているため、`DogRootView`のイニシャライザが高コストとなっています。
31+
32+
### `.task`でイニシャライザのコストを下げる
533
xxx

0 commit comments

Comments
 (0)