Skip to content

Commit ec018dc

Browse files
[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.
1 parent d7ddd5b commit ec018dc

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

GoogleDataTransport/GDTCORLibrary/GDTCORUploadCoordinator.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,16 @@ - (void)forceUploadForTarget:(GDTCORTarget)target {
6363
*/
6464
- (void)startTimer {
6565
dispatch_async(_coordinationQueue, ^{
66+
if (self->_timer) {
67+
// The timer has been already started.
68+
return;
69+
}
70+
6671
self->_timer =
6772
dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, self->_coordinationQueue);
6873
dispatch_source_set_timer(self->_timer, DISPATCH_TIME_NOW, self->_timerInterval,
6974
self->_timerLeeway);
75+
7076
dispatch_source_set_event_handler(self->_timer, ^{
7177
if (![[GDTCORApplication sharedApplication] isRunningInBackground]) {
7278
GDTCORUploadConditions conditions = [self uploadConditions];
@@ -83,6 +89,7 @@ - (void)startTimer {
8389
- (void)stopTimer {
8490
if (_timer) {
8591
dispatch_source_cancel(_timer);
92+
_timer = nil;
8693
}
8794
}
8895

GoogleDataTransport/GDTCORLibrary/Private/GDTCORUploadCoordinator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ NS_ASSUME_NONNULL_BEGIN
3939
@property(nonatomic, readonly) dispatch_queue_t coordinationQueue;
4040

4141
/** A timer that will causes regular checks for events to upload. */
42-
@property(nonatomic, readonly) dispatch_source_t timer;
42+
@property(nonatomic, readonly, nullable) dispatch_source_t timer;
4343

4444
/** The interval the timer will fire. */
4545
@property(nonatomic, readonly) uint64_t timerInterval;

GoogleDataTransport/GDTCORTests/Common/Categories/GDTCORUploadCoordinator+Testing.m

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,20 @@ - (void)reset {
3131

3232
- (void)setTimerInterval:(uint64_t)timerInterval {
3333
[self setValue:@(timerInterval) forKey:@"_timerInterval"];
34-
dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, timerInterval, self.timerLeeway);
34+
35+
dispatch_source_t timer = self.timer;
36+
if (timer) {
37+
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, timerInterval, self.timerLeeway);
38+
}
3539
}
3640

3741
- (void)setTimerLeeway:(uint64_t)timerLeeway {
3842
[self setValue:@(timerLeeway) forKey:@"_timerLeeway"];
39-
dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, self.timerInterval, timerLeeway);
43+
44+
dispatch_source_t timer = self.timer;
45+
if (timer) {
46+
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, self.timerInterval, timerLeeway);
47+
}
4048
}
4149

4250
@end

0 commit comments

Comments
 (0)