Skip to content

Commit 5910c2e

Browse files
author
Brennan Stehling
committed
implements new Storage API for handleEventsForBackgroundURLSession
1 parent 96b581c commit 5910c2e

File tree

8 files changed

+23
-34
lines changed

8 files changed

+23
-34
lines changed

Amplify/Categories/Storage/StorageCategory+ClientBehavior.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ extension StorageCategory: StorageCategoryBehavior {
5353
try await plugin.list(options: options)
5454
}
5555

56-
public func handleBackgroundEvents(identifier: String) async -> Bool {
57-
await plugin.handleBackgroundEvents(identifier: identifier)
56+
@discardableResult
57+
public func handleEventsForBackgroundURLSession(identifier: String) async -> Bool {
58+
await plugin.handleEventsForBackgroundURLSession(identifier: identifier)
5859
}
5960

6061
}

Amplify/Categories/Storage/StorageCategoryBehavior.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public protocol StorageCategoryBehavior {
8888
/// Handles background events which are related to URLSession
8989
/// - Parameter identifier: identifier
9090
/// - Returns: returns true if the identifier is handled by Amplify
91-
func handleBackgroundEvents(identifier: String) async -> Bool
91+
@discardableResult
92+
func handleEventsForBackgroundURLSession(identifier: String) async -> Bool
9293

9394
}

AmplifyPlugins/Storage/Sources/AWSS3StoragePlugin/AWSS3StoragePlugin+AsyncClientBehavior.swift

+2-4
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,8 @@ extension AWSS3StoragePlugin {
119119
return try await taskAdapter.value
120120
}
121121

122-
public func handleBackgroundEvents(identifier: String) async -> Bool {
123-
await withCheckedContinuation { (continuation: CheckedContinuation<Bool, Never>) in
124-
StorageBackgroundEventsRegistry.handleBackgroundEvents(identifier: identifier, continuation: continuation)
125-
}
122+
public func handleEventsForBackgroundURLSession(identifier: String) async -> Bool {
123+
await StorageBackgroundEventsRegistry.handleEventsForBackgroundURLSession(identifier: identifier)
126124
}
127125

128126
}

AmplifyPlugins/Storage/Sources/AWSS3StoragePlugin/Support/Internal/StorageBackgroundEventsRegistry.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ class StorageBackgroundEventsRegistry {
2222
/// - identifier: session identifier
2323
/// - completionHandler: completion handler
2424
/// - Returns: indicates if the identifier was registered and will be handled
25-
static func handleBackgroundEvents(identifier: String, continuation: StorageBackgroundEventsContinuation) {
26-
if self.identifier == identifier {
25+
static func handleEventsForBackgroundURLSession(identifier: String) async -> Bool {
26+
guard self.identifier == identifier else { return false }
27+
28+
return await withCheckedContinuation { (continuation: CheckedContinuation<Bool, Never>) in
2729
self.continuation = continuation
28-
} else {
29-
continuation.resume(returning: false)
3030
}
3131
}
3232

AmplifyPlugins/Storage/Sources/AWSS3StoragePlugin/Support/Internal/StorageServiceSessionDelegate.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ extension StorageServiceSessionDelegate: URLSessionDelegate {
5353
func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
5454
logURLSessionActivity("Session did finish background events")
5555

56-
if let identifier = storageService?.identifier,
56+
if let identifier = session.configuration.identifier,
5757
let continuation = StorageBackgroundEventsRegistry.getContinuation(for: identifier) {
5858
// Must be run on main thread as covered by Apple Developer docs.
5959
Task { @MainActor in

AmplifyPlugins/Storage/Tests/AWSS3StoragePluginTests/Support/Internal/StorageBackgroundEventsRegistryTests.swift

+9-20
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,21 @@ class StorageBackgroundEventsRegistryTests: XCTestCase {
2121
let done = asyncExpectation(description: "done", expectedFulfillmentCount: 2)
2222

2323
Task {
24-
let handled = await withCheckedContinuation { (continuation: CheckedContinuation<Bool, Never>) in
25-
StorageBackgroundEventsRegistry.handleBackgroundEvents(identifier: identifier, continuation: continuation)
26-
Task {
27-
await done.fulfill()
28-
}
29-
}
24+
let handled = await StorageBackgroundEventsRegistry.handleEventsForBackgroundURLSession(identifier: identifier)
25+
await done.fulfill()
3026
XCTAssertTrue(handled)
3127
}
3228

3329
Task {
34-
let otherHandled = await withCheckedContinuation { (continuation: CheckedContinuation<Bool, Never>) in
35-
StorageBackgroundEventsRegistry.handleBackgroundEvents(identifier: otherIdentifier, continuation: continuation)
36-
Task {
37-
await done.fulfill()
38-
}
39-
}
30+
let otherHandled = await StorageBackgroundEventsRegistry.handleEventsForBackgroundURLSession(identifier: otherIdentifier)
31+
await done.fulfill()
4032
XCTAssertFalse(otherHandled)
4133
}
4234

43-
await waitForExpectations([done])
44-
4535
handleEvents(for: identifier)
4636
handleEvents(for: otherIdentifier)
37+
38+
await waitForExpectations([done])
4739
}
4840

4941
func testHandlingUnregisteredIdentifier() async throws {
@@ -54,12 +46,8 @@ class StorageBackgroundEventsRegistryTests: XCTestCase {
5446
let done = asyncExpectation(description: "done")
5547

5648
Task {
57-
let handled = await withCheckedContinuation { (continuation: CheckedContinuation<Bool, Never>) in
58-
StorageBackgroundEventsRegistry.handleBackgroundEvents(identifier: identifier, continuation: continuation)
59-
Task {
60-
await done.fulfill()
61-
}
62-
}
49+
let handled = await StorageBackgroundEventsRegistry.handleEventsForBackgroundURLSession(identifier: identifier)
50+
await done.fulfill()
6351
XCTAssertFalse(handled)
6452
}
6553

@@ -70,6 +58,7 @@ class StorageBackgroundEventsRegistryTests: XCTestCase {
7058
func handleEvents(for identifier: String) {
7159
if let continuation = StorageBackgroundEventsRegistry.getContinuation(for: identifier) {
7260
continuation.resume(returning: true)
61+
StorageBackgroundEventsRegistry.removeContinuation(for: identifier)
7362
}
7463
}
7564

AmplifyTestCommon/Mocks/MockStorageCategoryPlugin.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class MockStorageCategoryPlugin: MessageReporter, StorageCategoryPlugin {
176176
return try await taskAdapter.value
177177
}
178178

179-
func handleBackgroundEvents(identifier: String) async -> Bool {
179+
func handleEventsForBackgroundURLSession(identifier: String) async -> Bool {
180180
false
181181
}
182182

AmplifyTests/CategoryTests/Hub/AmplifyOperationHubTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ class MockDispatchingStoragePlugin: StorageCategoryPlugin {
217217
return operation
218218
}
219219

220-
func handleBackgroundEvents(identifier: String) async -> Bool {
220+
func handleEventsForBackgroundURLSession(identifier: String) async -> Bool {
221221
false
222222
}
223223

0 commit comments

Comments
 (0)