Explorar o código

Move appear life cycle to placeholder

And also ensure image setting on main thread
onevcat %!s(int64=5) %!d(string=hai) anos
pai
achega
dde21468bb
Modificáronse 2 ficheiros con 15 adicións e 10 borrados
  1. 2 1
      Sources/SwiftUI/ImageBinder.swift
  2. 13 9
      Sources/SwiftUI/KFImage.swift

+ 2 - 1
Sources/SwiftUI/ImageBinder.swift

@@ -92,8 +92,9 @@ extension KFImage {
                             // by `Image.init(uiImage:)`. (The orientation information should be already contained in
                             // a `UIImage`)
                             // https://github.com/onevcat/Kingfisher/issues/1395
-                            self.image = value.image.kf.normalized
+                            let image = value.image.kf.normalized
                             DispatchQueue.main.async {
+                                self.image = image
                                 self.isLoaded.wrappedValue = true
                                 self.onSuccessDelegate.call(value)
                             }

+ 13 - 9
Sources/SwiftUI/KFImage.swift

@@ -101,19 +101,23 @@ public struct KFImage: SwiftUI.View {
                     }
                 }
                 .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
-                .onDisappear { [weak binder = self.binder] in
+                .onAppear { [weak binder] in
+                    guard let binder = binder else {
+                        return
+                    }
+                    if !binder.loadingOrSuccessed {
+                        binder.start()
+                    }
+                }
+                .onDisappear { [weak binder] in
+                    guard let binder = binder else {
+                        return
+                    }
                     if self.cancelOnDisappear {
-                        binder?.cancel()
+                        binder.cancel()
                     }
                 }
             }
-        }.onAppear { [weak binder] in
-            guard let binder = binder else {
-                return
-            }
-            if !binder.loadingOrSuccessed {
-                binder.start()
-            }
         }
     }
 }