Skip to content

Commit 59d233d

Browse files
committed
Try to workaround Apple Watch's issues about that runtime warning, other platform does not have this issue
1 parent 47fc670 commit 59d233d

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

SDWebImageSwiftUI/Classes/WebImage.swift

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,27 @@ public struct WebImage : View {
6060
.onDisappear {
6161
self.imageManager.cancel()
6262
}
63-
// Convert Combine.Publisher to Binding, I think this need a better API from Apple :)
64-
.onReceive(imageManager.$isLoading) { self.isLoading = $0 }
65-
.onReceive(imageManager.$progress) { self.progress = $0 }
63+
// Convert Combine.Publisher to Binding
64+
.onReceive(imageManager.$isLoading) { isLoading in
65+
// only Apple Watch complain that "Modifying state during view update, this will cause undefined behavior."
66+
// Use dispatch to workaround, Thanks Apple :)
67+
#if os(watchOS)
68+
DispatchQueue.main.async {
69+
self.isLoading = isLoading
70+
}
71+
#else
72+
self.isLoading = isLoading
73+
#endif
74+
}
75+
.onReceive(imageManager.$progress) { progress in
76+
#if os(watchOS)
77+
DispatchQueue.main.async {
78+
self.progress = progress
79+
}
80+
#else
81+
self.progress = progress
82+
#endif
83+
}
6684
if let indicator = indicator {
6785
return AnyView(
6886
ZStack {

0 commit comments

Comments
 (0)