Quellcode durchsuchen

Fix all warnings for target concurrency

onevcat vor 1 Jahr
Ursprung
Commit
eb56be566a

+ 2 - 2
Sources/Networking/RequestModifier.swift

@@ -72,7 +72,7 @@ public protocol AsyncImageDownloadRequestModifier: Sendable {
     /// method.
     /// method.
     ///
     ///
     /// User the ``DownloadTask`` value to track the task, or cancel it when you need to.
     /// User the ``DownloadTask`` value to track the task, or cancel it when you need to.
-    var onDownloadTaskStarted: ((DownloadTask?) -> Void)? { get }
+    var onDownloadTaskStarted: (@Sendable (DownloadTask?) -> Void)? { get }
 }
 }
 
 
 /// Represents and wraps a method for modifying a request before an image download request starts synchronously.
 /// Represents and wraps a method for modifying a request before an image download request starts synchronously.
@@ -119,7 +119,7 @@ public protocol ImageDownloadRequestModifier: AsyncImageDownloadRequestModifier
 extension ImageDownloadRequestModifier {
 extension ImageDownloadRequestModifier {
     /// This is `nil` for a sync `ImageDownloadRequestModifier` by default. You can get the `DownloadTask` from the
     /// This is `nil` for a sync `ImageDownloadRequestModifier` by default. You can get the `DownloadTask` from the
     /// return value of downloader method.
     /// return value of downloader method.
-    public var onDownloadTaskStarted: ((DownloadTask?) -> Void)? { return nil }
+    public var onDownloadTaskStarted: (@Sendable (DownloadTask?) -> Void)? { return nil }
 }
 }
 
 
 /// A wrapper for creating an ``ImageDownloadRequestModifier`` instance more easily.
 /// A wrapper for creating an ``ImageDownloadRequestModifier`` instance more easily.

+ 3 - 3
Sources/Networking/RetryStrategy.swift

@@ -72,7 +72,7 @@ public enum RetryDecision {
 }
 }
 
 
 /// Defines a retry strategy that can be applied to the ``KingfisherOptionsInfoItem/retryStrategy(_:)`` option.
 /// Defines a retry strategy that can be applied to the ``KingfisherOptionsInfoItem/retryStrategy(_:)`` option.
-public protocol RetryStrategy {
+public protocol RetryStrategy: Sendable {
 
 
     /// Kingfisher calls this method if an error occurs during the image retrieving process from ``KingfisherManager``.
     /// Kingfisher calls this method if an error occurs during the image retrieving process from ``KingfisherManager``.
     ///
     ///
@@ -92,7 +92,7 @@ public protocol RetryStrategy {
 public struct DelayRetryStrategy: RetryStrategy {
 public struct DelayRetryStrategy: RetryStrategy {
 
 
     /// Represents the interval mechanism used in a ``DelayRetryStrategy``.
     /// Represents the interval mechanism used in a ``DelayRetryStrategy``.
-    public enum Interval {
+    public enum Interval : Sendable{
         
         
         /// The next retry attempt should happen in a fixed number of seconds. 
         /// The next retry attempt should happen in a fixed number of seconds. 
         ///
         ///
@@ -108,7 +108,7 @@ public struct DelayRetryStrategy: RetryStrategy {
         /// Uses a block to determine the next interval.
         /// Uses a block to determine the next interval.
         ///
         ///
         /// The current retry count is given as a parameter.
         /// The current retry count is given as a parameter.
-        case custom(block: (_ retriedCount: Int) -> TimeInterval)
+        case custom(block: @Sendable (_ retriedCount: Int) -> TimeInterval)
 
 
         func timeInterval(for retriedCount: Int) -> TimeInterval {
         func timeInterval(for retriedCount: Int) -> TimeInterval {
             let retryAfter: TimeInterval
             let retryAfter: TimeInterval

+ 11 - 6
Tests/KingfisherTests/ImageDownloaderTests.swift

@@ -149,19 +149,24 @@ class ImageDownloaderTests: XCTestCase {
         let url = testURLs[0]
         let url = testURLs[0]
         stub(url, data: testImageData)
         stub(url, data: testImageData)
 
 
-        var downloadTaskCalled = false
+        let downloadTaskCalled = ActorBox(false)
 
 
         let asyncModifier = AsyncURLModifier(url: url, onDownloadTaskStarted: { task in
         let asyncModifier = AsyncURLModifier(url: url, onDownloadTaskStarted: { task in
             XCTAssertNotNil(task)
             XCTAssertNotNil(task)
-            downloadTaskCalled = true
+            Task {
+                await downloadTaskCalled.setValue(true)
+            }
         })
         })
 
 
         let someURL = URL(string: "some_strange_url")!
         let someURL = URL(string: "some_strange_url")!
         let task = downloader.downloadImage(with: someURL, options: [.requestModifier(asyncModifier)]) { result in
         let task = downloader.downloadImage(with: someURL, options: [.requestModifier(asyncModifier)]) { result in
             XCTAssertNotNil(result.value)
             XCTAssertNotNil(result.value)
             XCTAssertEqual(result.value?.url, url)
             XCTAssertEqual(result.value?.url, url)
-            XCTAssertTrue(downloadTaskCalled)
-            exp.fulfill()
+            Task {
+                let result = await downloadTaskCalled.value
+                XCTAssertTrue(result)
+                exp.fulfill()
+            }
         }
         }
         // The returned task is nil since the download is not starting immediately.
         // The returned task is nil since the download is not starting immediately.
         XCTAssertNil(task)
         XCTAssertNil(task)
@@ -708,9 +713,9 @@ final class URLModifier: ImageDownloadRequestModifier {
 
 
 final class AsyncURLModifier: AsyncImageDownloadRequestModifier {
 final class AsyncURLModifier: AsyncImageDownloadRequestModifier {
     let url: URL?
     let url: URL?
-    let onDownloadTaskStarted: ((DownloadTask?) -> Void)?
+    let onDownloadTaskStarted: (@Sendable (DownloadTask?) -> Void)?
     
     
-    init(url: URL?, onDownloadTaskStarted: ((DownloadTask?) -> Void)?) {
+    init(url: URL?, onDownloadTaskStarted: (@Sendable (DownloadTask?) -> Void)?) {
         self.url = url
         self.url = url
         self.onDownloadTaskStarted = onDownloadTaskStarted
         self.onDownloadTaskStarted = onDownloadTaskStarted
     }
     }

+ 7 - 2
Tests/KingfisherTests/RetryStrategyTests.swift

@@ -199,9 +199,14 @@ class RetryStrategyTests: XCTestCase {
 
 
 private struct E: Error {}
 private struct E: Error {}
 
 
-class StubRetryStrategy: RetryStrategy {
+final class StubRetryStrategy: RetryStrategy, @unchecked Sendable {
 
 
-    var count = 0
+    let queue = DispatchQueue(label: "com.onevcat.KingfisherTests.StubRetryStrategy")
+    var _count = 0
+    var count: Int {
+        get { queue.sync { _count } }
+        set { queue.sync { _count = newValue } }
+    }
 
 
     func retry(context: RetryContext, retryHandler: @escaping (RetryDecision) -> Void) {
     func retry(context: RetryContext, retryHandler: @escaping (RetryDecision) -> Void) {