@@ -436,7 +436,7 @@ - (NSString*)startViewInternal:(NSString *)viewName customSegmentation:(NSDictio
436436 segmentation[kCountlyVTKeySegment ] = CountlyDeviceInfo.osName ;
437437 segmentation[kCountlyVTKeyVisit ] = @1 ;
438438
439- if (self.isFirstView )
439+ if (self.isFirstView && [CountlyConnectionManager.sharedInstance isSessionStarted ] )
440440 {
441441 self.isFirstView = NO ;
442442 segmentation[kCountlyVTKeyStart ] = @1 ;
@@ -545,27 +545,33 @@ - (void)startStoppedViewsInternal
545545{
546546 // Create an array to store keys for views that need to be removed
547547 NSMutableArray <NSString *> *keysToRemove = [NSMutableArray array ];
548-
548+ NSMutableArray <NSString *> *keysToStart = [NSMutableArray array ];
549+
550+ // Collect keys without modifying the dictionary
549551 [self .viewDataDictionary enumerateKeysAndObjectsUsingBlock: ^(NSString * _Nonnull key, CountlyViewData * _Nonnull viewData, BOOL * _Nonnull stop) {
550552 if (viewData.willStartAgain )
551553 {
552- NSString *viewID = [self startViewInternal: viewData.viewName customSegmentation: viewData.startSegmentation isAutoStoppedView: viewData.isAutoStoppedView];
553-
554- // Retrieve the newly created viewData for the viewID
555- CountlyViewData* viewDataNew = self.viewDataDictionary [viewID];
556-
557- // Copy the segmentation data from the old view to the new view
558- viewDataNew.segmentation = viewData.segmentation .mutableCopy ;
559-
560- // Add the old view's ID to the array for removal later
554+ [keysToStart addObject: key];
561555 [keysToRemove addObject: viewData.viewID];
562556 }
563557 }];
564558
559+ // Start the collected views after enumeration
560+ for (NSString *key in keysToStart)
561+ {
562+ CountlyViewData *viewData = self.viewDataDictionary [key];
563+ NSString *viewID = [self startViewInternal: viewData.viewName customSegmentation: viewData.startSegmentation isAutoStoppedView: viewData.isAutoStoppedView];
564+
565+ // Retrieve and update the newly created viewData
566+ CountlyViewData *viewDataNew = self.viewDataDictionary [viewID];
567+ viewDataNew.segmentation = viewData.segmentation .mutableCopy ;
568+ }
569+
565570 // Remove the entries from the dictionary
566571 [self .viewDataDictionary removeObjectsForKeys: keysToRemove];
567572}
568573
574+
569575- (void )stopAllViewsInternal : (NSDictionary *)segmentation
570576{
571577 // TODO: Should apply all the segmenation operations here at one place instead of doing it for individual view
@@ -759,7 +765,7 @@ - (void)applicationWillTerminate {
759765
760766- (void )resetFirstView
761767{
762- self.isFirstView = NO ;
768+ self.isFirstView = YES ;
763769}
764770
765771
0 commit comments