Created
March 29, 2024 16:58
-
-
Save treastrain/cee21a1af3127c62123c8ac1560ce9c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct ContentView: View { | |
@StateObject private var object = Object() | |
var body: some View { | |
List(object.numbers, id: \.self) { number in | |
Text("Content \(number)") | |
} | |
.refreshable { | |
await object.update() | |
} | |
} | |
} | |
final class Object: ObservableObject { | |
@MainActor @Published var numbers: [Int] = [0] | |
func update() async { | |
try? await Task.sleep(for: .seconds(1)) | |
await addNewNumber() | |
} | |
@MainActor | |
private func addNewNumber() { | |
numbers.append(Int.random(in: 0..<100)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
適切かちょっと怪しいけど、再描画を期待していない冗長な更新がきっかけでbodyの再描画が起きてしまうことを防ぐなら