@@ -55,12 +55,10 @@ @interface SEGSegmentIntegration ()
55
55
@property (nonatomic , strong ) NSMutableArray *queue;
56
56
@property (nonatomic , strong ) NSDictionary *cachedStaticContext;
57
57
@property (nonatomic , strong ) NSURLSessionUploadTask *batchRequest;
58
- @property (nonatomic , assign ) BOOL batchRequestInProgress; // Thread confined to serialQueue.
59
58
@property (nonatomic , assign ) UIBackgroundTaskIdentifier flushTaskID;
60
59
@property (nonatomic , strong ) SEGReachability *reachability;
61
60
@property (nonatomic , strong ) NSTimer *flushTimer;
62
61
@property (nonatomic , strong ) dispatch_queue_t serialQueue;
63
- @property (nonatomic , strong ) dispatch_queue_t networkQueue;
64
62
@property (nonatomic , strong ) dispatch_queue_t backgroundTaskQueue;
65
63
@property (nonatomic , strong ) NSMutableDictionary *traits;
66
64
@property (nonatomic , assign ) SEGAnalytics *analytics;
@@ -89,9 +87,8 @@ - (id)initWithAnalytics:(SEGAnalytics *)analytics httpClient:(SEGHTTPClient *)ht
89
87
self.reachability = [SEGReachability reachabilityWithHostname: @" google.com" ];
90
88
[self .reachability startNotifier ];
91
89
self.cachedStaticContext = [self staticContext ];
92
- self.serialQueue = seg_dispatch_queue_create_specific (" com.segment.analytics.segment" , DISPATCH_QUEUE_SERIAL);
93
- self.networkQueue = seg_dispatch_queue_create_specific (" com.segment.analytics.segment.network" , DISPATCH_QUEUE_SERIAL);
94
- self.backgroundTaskQueue = seg_dispatch_queue_create_specific (" com.segment.analytics.segment.backgroundTask" , DISPATCH_QUEUE_SERIAL);
90
+ self.serialQueue = seg_dispatch_queue_create_specific (" io.segment.analytics.segmentio" , DISPATCH_QUEUE_SERIAL);
91
+ self.backgroundTaskQueue = seg_dispatch_queue_create_specific (" io.segment.analytics.backgroundTask" , DISPATCH_QUEUE_SERIAL);
95
92
self.flushTaskID = UIBackgroundTaskInvalid;
96
93
97
94
#if !TARGET_OS_TV
@@ -490,7 +487,7 @@ - (void)flushWithMaxSize:(NSUInteger)maxBatchSize
490
487
[self endBackgroundTask ];
491
488
return ;
492
489
}
493
- if (self.batchRequestInProgress ) {
490
+ if (self.batchRequest != nil ) {
494
491
SEGLog (@" %@ API request already in progress, not flushing again." , self);
495
492
return ;
496
493
}
@@ -511,7 +508,7 @@ - (void)flushQueueByLength
511
508
[self dispatchBackground: ^{
512
509
SEGLog (@" %@ Length is %lu ." , self, (unsigned long )self.queue .count );
513
510
514
- if (! self.batchRequestInProgress && [self .queue count ] >= self.configuration .flushAt ) {
511
+ if (self.batchRequest == nil && [self .queue count ] >= self.configuration .flushAt ) {
515
512
[self flush ];
516
513
}
517
514
}];
@@ -543,37 +540,31 @@ - (void)notifyForName:(NSString *)name userInfo:(id)userInfo
543
540
544
541
- (void )sendData : (NSArray *)batch
545
542
{
546
- if (self.batchRequestInProgress ) {
547
- SEGLog (@" API request already in progress, not flushing again." );
548
- return ;
549
- }
550
-
551
- self.batchRequestInProgress = true ;
543
+ NSMutableDictionary *payload = [[NSMutableDictionary alloc ] init ];
544
+ [payload setObject: iso8601FormattedString ([NSDate date ]) forKey: @" sentAt" ];
545
+ [payload setObject: batch forKey: @" batch" ];
552
546
553
- seg_dispatch_specific_async ( self. networkQueue , ^{
554
- SEGLog (@" %@ Flushing %lu of %lu queued API calls ." , self, ( unsigned long )batch. count , ( unsigned long )self. queue . count );
547
+ SEGLog ( @" %@ Flushing %lu of %lu queued API calls. " , self, ( unsigned long )batch. count , ( unsigned long )self. queue . count );
548
+ SEGLog (@" Flushing batch %@ ." , payload );
555
549
556
- self.batchRequest = [self .httpClient upload: batch forWriteKey: self .configuration.writeKey completionHandler: ^(BOOL retry) {
557
- [self dispatchBackground: ^{
558
- self.batchRequestInProgress = false ;
559
-
560
- if (retry) {
561
- [self notifyForName: SEGSegmentRequestDidFailNotification userInfo: batch];
562
- self.batchRequest = nil ;
563
- [self endBackgroundTask ];
564
- return ;
565
- }
566
-
567
- [self .queue removeObjectsInArray: batch];
568
- [self persistQueue ];
569
- [self notifyForName: SEGSegmentRequestDidSucceedNotification userInfo: batch];
550
+ self.batchRequest = [self .httpClient upload: payload forWriteKey: self .configuration.writeKey completionHandler: ^(BOOL retry) {
551
+ [self dispatchBackground: ^{
552
+ if (retry) {
553
+ [self notifyForName: SEGSegmentRequestDidFailNotification userInfo: batch];
570
554
self.batchRequest = nil ;
571
555
[self endBackgroundTask ];
572
- }];
556
+ return ;
557
+ }
558
+
559
+ [self .queue removeObjectsInArray: batch];
560
+ [self persistQueue ];
561
+ [self notifyForName: SEGSegmentRequestDidSucceedNotification userInfo: batch];
562
+ self.batchRequest = nil ;
563
+ [self endBackgroundTask ];
573
564
}];
565
+ }];
574
566
575
- [self notifyForName: SEGSegmentDidSendRequestNotification userInfo: batch];
576
- });
567
+ [self notifyForName: SEGSegmentDidSendRequestNotification userInfo: batch];
577
568
}
578
569
579
570
- (void )applicationDidEnterBackground
0 commit comments