Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 31 additions & 16 deletions Sources/Cache/ImageCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -868,31 +868,46 @@ open class ImageCache: @unchecked Sendable {
@objc public func backgroundCleanExpiredDiskCache() {
// if 'sharedApplication()' is unavailable, then return
guard let sharedApplication = KingfisherWrapper<UIApplication>.shared else { return }

let taskActor = ActorBox<UIBackgroundTaskIdentifier?>(nil)

let createdTask = sharedApplication.beginBackgroundTask(withName: "Kingfisher:backgroundCleanExpiredDiskCache") {
Task {
guard let bgTask = await taskActor.value, bgTask != .invalid else { return }
sharedApplication.endBackgroundTask(bgTask)
await taskActor.setValue(.invalid)

actor BackgroundTaskState {
private var value: UIBackgroundTaskIdentifier? = nil

func setValue(_ newValue: UIBackgroundTaskIdentifier) {
value = newValue
}

func takeValidValueAndInvalidate() -> UIBackgroundTaskIdentifier? {
guard let task = value, task != .invalid else { return nil }
value = .invalid
return task
}
}

cleanExpiredDiskCache {
Task {
guard let bgTask = await taskActor.value, bgTask != .invalid else { return }

let taskState = BackgroundTaskState()

let endBackgroundTaskIfNeeded: @MainActor @Sendable () -> Void = {
Task { @MainActor in
guard let bgTask = await taskState.takeValidValueAndInvalidate() else { return }
guard let sharedApplication = KingfisherWrapper<UIApplication>.shared else { return }
#if compiler(>=6)
sharedApplication.endBackgroundTask(bgTask)
#else
await sharedApplication.endBackgroundTask(bgTask)
#endif
await taskActor.setValue(.invalid)
}
}

Task {
await taskActor.setValue(createdTask)

let createdTask = sharedApplication.beginBackgroundTask(
withName: "Kingfisher:backgroundCleanExpiredDiskCache",
expirationHandler: endBackgroundTaskIfNeeded
)

Task { await taskState.setValue(createdTask) }

cleanExpiredDiskCache {
Task { @MainActor in
endBackgroundTaskIfNeeded()
}
}
}
#endif
Expand Down
11 changes: 0 additions & 11 deletions Sources/Utility/Box.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,3 @@ class Box<T> {
self.value = value
}
}

actor ActorBox<T> {
var value: T
init(_ value: T) {
self.value = value
}

func setValue(_ value: T) {
self.value = value
}
}
47 changes: 15 additions & 32 deletions Tests/KingfisherTests/KingfisherManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1243,31 +1243,25 @@ class KingfisherManagerTests: XCTestCase {

stub(url, data: testImageData)

let task = ActorBox<DownloadTask?>(nil)

let called = ActorBox(false)
let task = LockIsolated<DownloadTask?>(nil)
let callbackCount = LockIsolated(0)

let t: DownloadTask? = manager.retrieveImage(with: url) { result in
Task {
let calledResult = await called.value
XCTAssertFalse(calledResult)
XCTAssertNotNil(result.value?.image)

if !calledResult {
Task {
await task.value?.cancel()
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
exp.fulfill()
}
} else {
XCTFail("Callback should not be invoked again.")
}
let count = callbackCount.withValue { value in
value += 1
return value
}

XCTAssertEqual(count, 1, "Callback should not be invoked again.")
XCTAssertNotNil(result.value?.image)

task.value?.cancel()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
exp.fulfill()
}
}
Task {
await task.setValue(t)
}

task.setValue(t)
waitForExpectations(timeout: 3, handler: nil)
}

Expand Down Expand Up @@ -1789,17 +1783,6 @@ struct SimpleImageDataProvider: ImageDataProvider, @unchecked Sendable {
struct E: Error {}
}

actor ActorBox<T> {
var value: T
init(_ value: T) {
self.value = value
}

func setValue(_ value: T) {
self.value = value
}
}

actor ActorArray<Element> {
var value: [Element]
init(_ value: [Element]) {
Expand Down
8 changes: 1 addition & 7 deletions Tests/KingfisherTests/RetryStrategyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ class RetryStrategyTests: XCTestCase {

func testDelayRetryStrategyDidRetried() {
let exp = expectation(description: #function)
let called = ActorBox(false)
let source = Source.network(URL(string: "url")!)
let retry = DelayRetryStrategy(maxRetryCount: 3, retryInterval: .seconds(0))
let context = RetryContext(
Expand All @@ -227,12 +226,7 @@ class RetryStrategyTests: XCTestCase {
XCTFail("The decision should be `retry`.")
return
}
Task {
await called.setValue(true)
let result = await called.value
XCTAssertTrue(result)
exp.fulfill()
}
exp.fulfill()
}

waitForExpectations(timeout: 3, handler: nil)
Expand Down