Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #454 from rusik/patch-1
Browse files Browse the repository at this point in the history
Update 10160.md
  • Loading branch information
Jeehut authored Nov 20, 2023
2 parents 636310d + bd64bca commit 60ba422
Showing 1 changed file with 80 additions and 58 deletions.
138 changes: 80 additions & 58 deletions content/notes/wwdc23/10160.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
contributors: RamitSharma991
contributors: RamitSharma991, rusik
---

* SwiftUI makes it easy to build complex, powerful apps, offering a large set of features and complex controls like lists and tables.
Expand Down Expand Up @@ -35,23 +35,24 @@ contributors: RamitSharma991

```swift
struct ScalableDogImage: View {
@State private var scaleToFill = false
var dog: Dog
var body: some View {
let _ = Self._printChanges()
// it is never guaranteed to always exist and may even be removed in a future release,
@State private var scaleToFill = false
var dog: Dog

var body: some View {

// It is never guaranteed to always exist and may even be removed in a future release,
// so never submit a call to this method to the app store.
dog.image
.resizable()
.aspectRatio(
contentMode: scaleToFill ? .fill : .fit)
.frame(maxHeight: scaleToFill ? 500 : nil)
.padding(.vertical, 16)
.onTapGesture {
withAnimation { scaleToFill.toggle() }
}
}
let _ = Self._printChanges()

dog.image
.resizable()
.aspectRatio(contentMode: scaleToFill ? .fill : .fit)
.frame(maxHeight: scaleToFill ? 500 : nil)
.padding(.vertical, 16)
.onTapGesture {
withAnimation { scaleToFill.toggle() }
}
}
}
```

Expand Down Expand Up @@ -81,46 +82,45 @@ struct ScalableDogImage: View {
// which brings us to the initializer, which fetches the list of dogs.
// As the code comment says, this could take a long time. This is synchronous work.
struct DogRootView: View {
@State private var model = FetchModel()
var body: some View {
DogList(model.dogs)
}
@State private var model = FetchModel()
var body: some View {
DogList(model.dogs)
}
}

@Observable class FetchModel {
var dogs: [Dog]
init() {
fetchDogs()
}
var dogs: [Dog]
init() {
fetchDogs()
}
}
func fetchDogs() {
// Takes a long time
}
func fetchDogs() {
// Takes a long time
}
}

//// Updated and fixed using task modifier, asynchronous fetch and awaiting.
struct DogRootView: View {
@State private var model = FetchModel()
var body: some View {
DogList(model.dogs)
.task { await model.fetchDogs() }
}
// Updated and fixed using task modifier, asynchronous fetch and awaiting.
struct DogRootView: View {
@State private var model = FetchModel()
var body: some View {
DogList(model.dogs)
.task { await model.fetchDogs() }
}
}

@Observable class FetchModel {
var dogs: [Dog]
init() {}
func fetchDogs() async {
// Takes a long time
}
var dogs: [Dog]
init() {}
func fetchDogs() async {
// Takes a long time
}
}

```

## Other hidden sources of work
Expand Down Expand Up @@ -160,19 +160,41 @@ struct DogRootView: View {

```swift

// Lists
// unknown views per element
List {
ForEach(dogs) {
DogCell(dog: $0)
}
// Constant views per element - ✅
List {
ForEach(dogs) { dog in
DogCell(dog)
}
}

// Variable views per element - ⚠️
List {
ForEach(dogs) { dog in
if dog.fetchToy == .ball {
DogCell(dog)
}
}
}

// Unknown views per element - ⚠️
List {
ForEach(dogs) { dog in
AnyView(...)
}
}

// Avoid inline filtering - ⚠️
List {
ForEach(dogs.filter(...)) { dog in
DogCell(dog)
}
}

// Lists Fixed with cached filter and constant views per element
List {
ForEach(tennisBallDogs) { dog in
DogCell(dog)
}
// Cached filter and constant views per element - ✅
List {
ForEach(tennisBallDogs) { dog in
DogCell(dog)
}
}
```

Expand Down

0 comments on commit 60ba422

Please sign in to comment.