Kaynağa Gözat

[GDTCORUploadCoordinator startTimer]: don't restart timer if already started (#6032)

* [GDTCORUploadCoordinator startTimer]: don't restart timer if already started

* style

* `GDTCORUploadCoordinator.timer` - nullable

* GDTCORUploadCoordinator test utils thread safety.
Maksym Malyhin 5 yıl önce
ebeveyn
işleme
ec018dcbd7

+ 7 - 0
GoogleDataTransport/GDTCORLibrary/GDTCORUploadCoordinator.m

@@ -63,10 +63,16 @@
  */
 - (void)startTimer {
   dispatch_async(_coordinationQueue, ^{
+    if (self->_timer) {
+      // The timer has been already started.
+      return;
+    }
+
     self->_timer =
         dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, self->_coordinationQueue);
     dispatch_source_set_timer(self->_timer, DISPATCH_TIME_NOW, self->_timerInterval,
                               self->_timerLeeway);
+
     dispatch_source_set_event_handler(self->_timer, ^{
       if (![[GDTCORApplication sharedApplication] isRunningInBackground]) {
         GDTCORUploadConditions conditions = [self uploadConditions];
@@ -83,6 +89,7 @@
 - (void)stopTimer {
   if (_timer) {
     dispatch_source_cancel(_timer);
+    _timer = nil;
   }
 }
 

+ 1 - 1
GoogleDataTransport/GDTCORLibrary/Private/GDTCORUploadCoordinator.h

@@ -39,7 +39,7 @@ NS_ASSUME_NONNULL_BEGIN
 @property(nonatomic, readonly) dispatch_queue_t coordinationQueue;
 
 /** A timer that will causes regular checks for events to upload. */
-@property(nonatomic, readonly) dispatch_source_t timer;
+@property(nonatomic, readonly, nullable) dispatch_source_t timer;
 
 /** The interval the timer will fire. */
 @property(nonatomic, readonly) uint64_t timerInterval;

+ 10 - 2
GoogleDataTransport/GDTCORTests/Common/Categories/GDTCORUploadCoordinator+Testing.m

@@ -31,12 +31,20 @@
 
 - (void)setTimerInterval:(uint64_t)timerInterval {
   [self setValue:@(timerInterval) forKey:@"_timerInterval"];
-  dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, timerInterval, self.timerLeeway);
+
+  dispatch_source_t timer = self.timer;
+  if (timer) {
+    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, timerInterval, self.timerLeeway);
+  }
 }
 
 - (void)setTimerLeeway:(uint64_t)timerLeeway {
   [self setValue:@(timerLeeway) forKey:@"_timerLeeway"];
-  dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, self.timerInterval, timerLeeway);
+
+  dispatch_source_t timer = self.timer;
+  if (timer) {
+    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, self.timerInterval, timerLeeway);
+  }
 }
 
 @end