Skip to content

Instantly share code, notes, and snippets.

@treastrain
Created March 29, 2024 16:58
Show Gist options
  • Save treastrain/cee21a1af3127c62123c8ac1560ce9c0 to your computer and use it in GitHub Desktop.
Save treastrain/cee21a1af3127c62123c8ac1560ce9c0 to your computer and use it in GitHub Desktop.
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))
}
}
@shimastripe
Copy link

struct ContentView: View, Equatable {
    static func == (lhs: ContentView, rhs: ContentView) -> Bool {
        true
    }
    @StateObject private var exploreVM = ExploreViewModel()
}

適切かちょっと怪しいけど、再描画を期待していない冗長な更新がきっかけでbodyの再描画が起きてしまうことを防ぐなら

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment