Skip to content

Commit bd4e1f0

Browse files
committed
[Core] Prefer completion handler APIs in Core Internal
1 parent 13affce commit bd4e1f0

File tree

4 files changed

+68
-66
lines changed

4 files changed

+68
-66
lines changed

FirebaseCore/Internal/Sources/HeartbeatLogging/HeartbeatController.swift

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -126,34 +126,30 @@ public final class HeartbeatController {
126126
}
127127
}
128128

129-
@available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, watchOS 6.0, *)
130-
public func flushAsync() async -> HeartbeatsPayload {
131-
return await withCheckedContinuation { continuation in
132-
let resetTransform = { (heartbeatsBundle: HeartbeatsBundle?) -> HeartbeatsBundle? in
133-
guard let oldHeartbeatsBundle = heartbeatsBundle else {
134-
return nil // Storage was empty.
135-
}
136-
// The new value that's stored will use the old's cache to prevent the
137-
// logging of duplicates after flushing.
138-
return HeartbeatsBundle(
139-
capacity: self.heartbeatsStorageCapacity,
140-
cache: oldHeartbeatsBundle.lastAddedHeartbeatDates
141-
)
129+
public func flushAsync(completionHandler: @escaping (HeartbeatsPayload) -> Void) {
130+
let resetTransform = { (heartbeatsBundle: HeartbeatsBundle?) -> HeartbeatsBundle? in
131+
guard let oldHeartbeatsBundle = heartbeatsBundle else {
132+
return nil // Storage was empty.
142133
}
134+
// The new value that's stored will use the old's cache to prevent the
135+
// logging of duplicates after flushing.
136+
return HeartbeatsBundle(
137+
capacity: self.heartbeatsStorageCapacity,
138+
cache: oldHeartbeatsBundle.lastAddedHeartbeatDates
139+
)
140+
}
143141

144-
// Asynchronously gets and returns the stored heartbeats, resetting storage
145-
// using the given transform.
146-
storage.getAndSetAsync(using: resetTransform) { result in
147-
switch result {
148-
case let .success(heartbeatsBundle):
149-
// If no heartbeats bundle was stored, return an empty payload.
150-
continuation
151-
.resume(returning: heartbeatsBundle?.makeHeartbeatsPayload() ?? HeartbeatsPayload
152-
.emptyPayload)
153-
case .failure:
154-
// If the operation throws, assume no heartbeat(s) were retrieved or set.
155-
continuation.resume(returning: HeartbeatsPayload.emptyPayload)
156-
}
142+
// Asynchronously gets and returns the stored heartbeats, resetting storage
143+
// using the given transform.
144+
storage.getAndSetAsync(using: resetTransform) { result in
145+
switch result {
146+
case let .success(heartbeatsBundle):
147+
// If no heartbeats bundle was stored, return an empty payload.
148+
completionHandler(heartbeatsBundle?.makeHeartbeatsPayload() ?? HeartbeatsPayload
149+
.emptyPayload)
150+
case .failure:
151+
// If the operation throws, assume no heartbeat(s) were retrieved or set.
152+
completionHandler(HeartbeatsPayload.emptyPayload)
157153
}
158154
}
159155
}

FirebaseCore/Internal/Sources/HeartbeatLogging/_ObjC_HeartbeatController.swift

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,8 @@ public class _ObjC_HeartbeatController: NSObject {
5252
public func flushAsync(completionHandler: @escaping (_ObjC_HeartbeatsPayload) -> Void) {
5353
// TODO: When minimum version moves to iOS 13.0, restore the async version
5454
// removed in #13952.
55-
if #available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, watchOS 6.0, *) {
56-
Task {
57-
let heartbeatsPayload = await heartbeatController.flushAsync()
58-
completionHandler(_ObjC_HeartbeatsPayload(heartbeatsPayload))
59-
}
60-
} else {
61-
// It is not expected to reach this state as this API should only be
62-
// called on iOS 13.0+.
63-
completionHandler(_ObjC_HeartbeatsPayload(HeartbeatsPayload.emptyPayload))
55+
heartbeatController.flushAsync { heartbeatsPayload in
56+
completionHandler(_ObjC_HeartbeatsPayload(heartbeatsPayload))
6457
}
6558
}
6659

FirebaseCore/Internal/Tests/Integration/HeartbeatLoggingIntegrationTests.swift

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,29 +52,36 @@ class HeartbeatLoggingIntegrationTests: XCTestCase {
5252
)
5353
}
5454

55-
@available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, watchOS 6.0, *)
56-
func testLogAndFlushAsync() async throws {
55+
func testLogAndFlushAsync() throws {
5756
// Given
5857
let heartbeatController = HeartbeatController(id: #function)
5958
let expectedDate = HeartbeatsPayload.dateFormatter.string(from: Date())
59+
let expectation = self.expectation(description: #function)
6060
// When
6161
heartbeatController.log("dummy_agent")
62-
let payload = await heartbeatController.flushAsync()
63-
// Then
64-
try HeartbeatLoggingTestUtils.assertEqualPayloadStrings(
65-
payload.headerValue(),
66-
"""
67-
{
68-
"version": 2,
69-
"heartbeats": [
62+
heartbeatController.flushAsync { payload in
63+
// Then
64+
do {
65+
try HeartbeatLoggingTestUtils.assertEqualPayloadStrings(
66+
payload.headerValue(),
67+
"""
7068
{
71-
"agent": "dummy_agent",
72-
"dates": ["\(expectedDate)"]
69+
"version": 2,
70+
"heartbeats": [
71+
{
72+
"agent": "dummy_agent",
73+
"dates": ["\(expectedDate)"]
74+
}
75+
]
7376
}
74-
]
77+
"""
78+
)
79+
expectation.fulfill()
80+
} catch {
81+
XCTFail("Unexpected error: \(error)")
7582
}
76-
"""
77-
)
83+
}
84+
waitForExpectations(timeout: 1.0)
7885
}
7986

8087
/// This test may flake if it is executed during the transition from one day to the next.

FirebaseCore/Internal/Tests/Unit/HeartbeatControllerTests.swift

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,35 +58,41 @@ class HeartbeatControllerTests: XCTestCase {
5858
assertHeartbeatControllerFlushesEmptyPayload(controller)
5959
}
6060

61-
@available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, watchOS 6.0, *)
62-
func testLogAndFlushAsync() async throws {
61+
func testLogAndFlushAsync() throws {
6362
// Given
6463
let controller = HeartbeatController(
6564
storage: HeartbeatStorageFake(),
6665
dateProvider: { self.date }
6766
)
67+
let expectation = expectation(description: #function)
6868

6969
assertHeartbeatControllerFlushesEmptyPayload(controller)
7070

7171
// When
7272
controller.log("dummy_agent")
73-
let heartbeatPayload = await controller.flushAsync()
74-
75-
// Then
76-
try HeartbeatLoggingTestUtils.assertEqualPayloadStrings(
77-
heartbeatPayload.headerValue(),
78-
"""
79-
{
80-
"version": 2,
81-
"heartbeats": [
73+
controller.flushAsync { heartbeatPayload in
74+
// Then
75+
do {
76+
try HeartbeatLoggingTestUtils.assertEqualPayloadStrings(
77+
heartbeatPayload.headerValue(),
78+
"""
8279
{
83-
"agent": "dummy_agent",
84-
"dates": ["2021-11-01"]
80+
"version": 2,
81+
"heartbeats": [
82+
{
83+
"agent": "dummy_agent",
84+
"dates": ["2021-11-01"]
85+
}
86+
]
8587
}
86-
]
88+
"""
89+
)
90+
expectation.fulfill()
91+
} catch {
92+
XCTFail("Unexpected error: \(error)")
8793
}
88-
"""
89-
)
94+
}
95+
waitForExpectations(timeout: 1.0)
9096

9197
assertHeartbeatControllerFlushesEmptyPayload(controller)
9298
}

0 commit comments

Comments
 (0)