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)) | |
} | |
} |
Task.sleep(for:tolerance:clock:)
のエラーを無視しないパターン
import SwiftUI
@MainActor
final class Object: ObservableObject {
@Published var numbers: [Int] = [0]
nonisolated func update() async {
Task { @MainActor in
numbers.removeAll()
}
do {
try await Task.sleep(for: .seconds(1))
Task { @MainActor in
numbers.append(Int.random(in: 0..<100))
}
} catch {
print(error)
}
}
}
struct ContentView: View {
@StateObject private var object = Object()
var body: some View {
NavigationStack {
List(object.numbers, id: \.self) { number in
Text("Content \(number)")
}
.refreshable {
await object.update()
}
}
}
}
Task.value
を await するパターン
import SwiftUI
@MainActor
final class Object: ObservableObject {
@Published var numbers: [Int] = [0]
nonisolated func update() async {
await Task { @MainActor in
numbers.removeAll()
}.value
try? await Task.sleep(for: .seconds(1))
await Task { @MainActor in
numbers.append(Int.random(in: 0..<100))
}.value
}
}
struct ContentView: View {
@StateObject private var object = Object()
var body: some View {
NavigationStack {
List(object.numbers, id: \.self) { number in
Text("Content \(number)")
}
.refreshable {
await object.update()
}
}
}
}
struct Quote: Codable, Identifiable {
let text: String?
let author: String?
var id: String {
text ?? ""
}
}
メインスレッドをパンパンにして 画面描画にも影響させるコード
import Foundation
import SwiftUI
func unsafeSleep() {
for n in 0..<Int.max {
_ = sin(Double(n))
}
}
@MainActor
final class Object: ObservableObject {
@Published var numbers: [Int] = [0]
func update() async {
Task { @MainActor in
numbers.removeAll()
}
do {
// try await Task.sleep(for: .seconds(1))
unsafeSleep()
Task { @MainActor in
numbers.append(Int.random(in: 0..<100))
}
} catch {
print(error)
}
}
}
struct ContentView: View {
@StateObject private var object = Object()
var body: some View {
NavigationStack {
List(object.numbers, id: \.self) { number in
Text("Content \(number)")
}
.refreshable {
await object.update()
}
}
}
}
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
Object.update()
に処理をつっこんで、Task
を作るパターン(これは 下に引っ張って更新 するとアニメーションが消えちゃう)