Browse Source

Merge pull request #1422 from onevcat/fix/weak-binder

Replace unowned with weak references
Wei Wang 5 năm trước cách đây
mục cha
commit
78b1873c4b
2 tập tin đã thay đổi với 11 bổ sung4 xóa
  1. 3 1
      Sources/Networking/SessionDelegate.swift
  2. 8 3
      Sources/SwiftUI/KFImage.swift

+ 3 - 1
Sources/Networking/SessionDelegate.swift

@@ -62,7 +62,9 @@ class SessionDelegate: NSObject {
 
         // Create a new task if necessary.
         let task = SessionDataTask(task: dataTask)
-        task.onCallbackCancelled.delegate(on: self) { [unowned task] (self, value) in
+        task.onCallbackCancelled.delegate(on: self) { [weak task] (self, value) in
+            guard let task = task else { return }
+
             let (token, callback) = value
 
             let error = KingfisherError.requestError(reason: .taskCancelled(task: task, token: token))

+ 8 - 3
Sources/SwiftUI/KFImage.swift

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