@@ -37,7 +37,7 @@ @interface FPRGaugeManager () <FPRCPUGaugeCollectorDelegate, FPRMemoryGaugeColle
3737@property (nonatomic ) NSMutableArray *gaugeData;
3838
3939/* * @brief Currently active sessionID. */
40- @property (nonatomic , readwrite ) NSString *currentSessionId;
40+ @property (nonatomic , readwrite , copy ) NSString *currentSessionId;
4141
4242@end
4343
@@ -117,7 +117,8 @@ - (BOOL)gaugeCollectionEnabled {
117117}
118118
119119- (void )startCollectingGauges : (FPRGauges)gauges forSessionId : (NSString *)sessionId {
120- [self prepareAndDispatchGaugeData ];
120+ // Dispatch the already available gauge data with old sessionId.
121+ [self prepareAndDispatchCollectedGaugeDataWithSessionId: self .currentSessionId];
121122
122123 self.currentSessionId = sessionId;
123124 if (self.gaugeCollectionEnabled ) {
@@ -143,7 +144,8 @@ - (void)stopCollectingGauges:(FPRGauges)gauges {
143144
144145 self.activeGauges = self.activeGauges & ~(gauges);
145146
146- [self prepareAndDispatchGaugeData ];
147+ // Flush out all the already collected gauge metrics
148+ [self prepareAndDispatchCollectedGaugeDataWithSessionId: self .currentSessionId];
147149}
148150
149151- (void )collectAllGauges {
@@ -172,31 +174,33 @@ - (void)dispatchMetric:(id)gaugeMetric {
172174
173175#pragma mark - Utils
174176
175- - (void )prepareAndDispatchGaugeData {
176- NSArray *currentBatch = self.gaugeData ;
177- NSString *currentSessionId = self.currentSessionId ;
178- self.gaugeData = [[NSMutableArray alloc ] init ];
179- dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
180- if (currentBatch.count > 0 ) {
181- [[FPRClient sharedInstance ] logGaugeMetric: currentBatch forSessionId: currentSessionId];
182- FPRLogDebug (kFPRGaugeManagerDataCollected , @" Logging %lu gauge metrics." ,
183- (unsigned long )currentBatch.count );
184- }
177+ - (void )prepareAndDispatchCollectedGaugeDataWithSessionId : (nullable NSString *)sessionId {
178+ dispatch_async (self.gaugeDataProtectionQueue , ^{
179+ NSArray *dispatchGauges = [self .gaugeData copy ];
180+ self.gaugeData = [[NSMutableArray alloc ] init ];
181+
182+ dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
183+ if (dispatchGauges.count > 0 && sessionId != nil ) {
184+ [[FPRClient sharedInstance ] logGaugeMetric: dispatchGauges forSessionId: sessionId];
185+ FPRLogDebug (kFPRGaugeManagerDataCollected , @" Logging %lu gauge metrics." ,
186+ (unsigned long )dispatchGauges.count );
187+ }
188+ });
185189 });
186190}
187191
188192/* *
189193 * Adds the gauge to the batch and decide on when to dispatch the events to Google Data Transport.
190194 *
191- * @param gaugeData Gauge data received from the collectors.
195+ * @param gauge Gauge data received from the collectors.
192196 */
193- - (void )addGaugeData : (id )gaugeData {
197+ - (void )addGaugeData : (id )gauge {
194198 dispatch_async (self.gaugeDataProtectionQueue , ^{
195- if (gaugeData ) {
196- [self .gaugeData addObject: gaugeData ];
199+ if (gauge ) {
200+ [self .gaugeData addObject: gauge ];
197201
198202 if (self.gaugeData .count >= kGaugeDataBatchSize ) {
199- [self prepareAndDispatchGaugeData ];
203+ [self prepareAndDispatchCollectedGaugeDataWithSessionId: self .currentSessionId ];
200204 }
201205 }
202206 });
0 commit comments