Skip to content

Commit a14dd48

Browse files
authored
[Core] Add async flush method (#13851)
1 parent 7bd6887 commit a14dd48

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

FirebaseCore/Extension/FIRHeartbeatLogger.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,23 @@ NSString *_Nullable FIRHeaderValueFromHeartbeatsPayload(FIRHeartbeatsPayload *he
6868
- (void)log;
6969

7070
#ifndef FIREBASE_BUILD_CMAKE
71-
/// Flushes heartbeats from storage into a structured payload of heartbeats.
71+
/// Synchronously flushes heartbeats from storage into a structured payload of heartbeats.
7272
///
7373
/// This API is for clients using platform logging V2.
7474
///
7575
/// @note This API is thread-safe.
7676
/// @return A payload of heartbeats.
7777
- (FIRHeartbeatsPayload *)flushHeartbeatsIntoPayload;
78+
79+
/// Asynchronously flushes heartbeats from storage into a structured payload of heartbeats.
80+
///
81+
/// This API is for clients using platform logging V2.
82+
///
83+
/// @note This API is thread-safe.
84+
/// @param completionHandler A completion handler to process the flushed payload of heartbeats.
85+
- (void)flushHeartbeatsIntoPayloadWithCompletionHandler:
86+
(void (^)(FIRHeartbeatsPayload *))completionHandler
87+
API_AVAILABLE(ios(13.0), macosx(10.15), macCatalyst(13.0), tvos(13.0), watchos(6.0));
7888
#endif // FIREBASE_BUILD_CMAKE
7989

8090
/// Gets today's corresponding heartbeat code.

FirebaseCore/Sources/FIRHeartbeatLogger.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ - (FIRHeartbeatsPayload *)flushHeartbeatsIntoPayload {
7878
FIRHeartbeatsPayload *payload = [_heartbeatController flush];
7979
return payload;
8080
}
81+
82+
- (void)flushHeartbeatsIntoPayloadWithCompletionHandler:
83+
(void (^)(FIRHeartbeatsPayload *))completionHandler {
84+
[_heartbeatController flushAsyncWithCompletionHandler:^(FIRHeartbeatsPayload *payload) {
85+
completionHandler(payload);
86+
}];
87+
}
8188
#endif // FIREBASE_BUILD_CMAKE
8289

8390
- (FIRDailyHeartbeatCode)heartbeatCodeForToday {

FirebaseCore/Tests/Unit/FIRHeartbeatLoggerTests.m

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,30 @@ - (void)testFlushing_UsingV2API_WhenHeartbeatsAreStored_ReturnsNonEmptyPayload {
125125
}];
126126
}
127127

128+
- (void)testFlushingAsync_UsingV2API_WhenHeartbeatsAreStored_ReturnsNonEmptyPayload API_AVAILABLE(
129+
ios(13.0), macosx(10.15), macCatalyst(13.0), tvos(13.0), watchos(6.0)) {
130+
// Given
131+
FIRHeartbeatLogger *heartbeatLogger = self.heartbeatLogger;
132+
NSString *expectedDate = [[self class] formattedStringForDate:[NSDate date]];
133+
// When
134+
[heartbeatLogger log];
135+
XCTestExpectation *expectation = [self expectationWithDescription:@"async flush"];
136+
[heartbeatLogger
137+
flushHeartbeatsIntoPayloadWithCompletionHandler:^(FIRHeartbeatsPayload *heartbeatsPayload) {
138+
// Then
139+
[self assertEncodedPayloadHeader:FIRHeaderValueFromHeartbeatsPayload(heartbeatsPayload)
140+
isEqualToPayloadJSON:@{
141+
@"version" : @2,
142+
@"heartbeats" : @[
143+
@{@"agent" : @"dummy_agent",
144+
@"dates" : @[ expectedDate ]}
145+
]
146+
}];
147+
[expectation fulfill];
148+
}];
149+
[self waitForExpectations:@[ expectation ] timeout:1.0];
150+
}
151+
128152
- (void)testFlushing_UsingV2API_WhenNoHeartbeatsAreStored_ReturnsEmptyPayload {
129153
// Given
130154
FIRHeartbeatLogger *heartbeatLogger = self.heartbeatLogger;
@@ -134,6 +158,21 @@ - (void)testFlushing_UsingV2API_WhenNoHeartbeatsAreStored_ReturnsEmptyPayload {
134158
[self assertHeartbeatsPayloadIsEmpty:heartbeatsPayload];
135159
}
136160

161+
- (void)testFlushingAsync_UsingV2API_WhenNoHeartbeatsAreStored_ReturnsEmptyPayload API_AVAILABLE(
162+
ios(13.0), macosx(10.15), macCatalyst(13.0), tvos(13.0), watchos(6.0)) {
163+
// Given
164+
FIRHeartbeatLogger *heartbeatLogger = self.heartbeatLogger;
165+
// When
166+
XCTestExpectation *expectation = [self expectationWithDescription:@"async flush"];
167+
[heartbeatLogger
168+
flushHeartbeatsIntoPayloadWithCompletionHandler:^(FIRHeartbeatsPayload *heartbeatsPayload) {
169+
// Then
170+
[self assertHeartbeatsPayloadIsEmpty:heartbeatsPayload];
171+
[expectation fulfill];
172+
}];
173+
[self waitForExpectations:@[ expectation ] timeout:1.0];
174+
}
175+
137176
- (void)testLogAndFlushUsingV1API_AndThenFlushAgainUsingV2API_FlushesHeartbeatInTheFirstFlush {
138177
// Given
139178
FIRHeartbeatLogger *heartbeatLogger = self.heartbeatLogger;

0 commit comments

Comments
 (0)