1616
1717#import " Crashlytics/Crashlytics/Controllers/FIRCLSManagerData.h"
1818#import " Crashlytics/Crashlytics/Controllers/FIRCLSReportUploader.h"
19+ #import " Crashlytics/Crashlytics/DataCollection/FIRCLSDataCollectionArbiter.h"
1920#import " Crashlytics/Crashlytics/DataCollection/FIRCLSDataCollectionToken.h"
2021#import " Crashlytics/Crashlytics/Helpers/FIRCLSLogger.h"
2122#import " Crashlytics/Crashlytics/Models/FIRCLSFileManager.h"
2223#import " Crashlytics/Crashlytics/Models/FIRCLSInternalReport.h"
24+ #import " Crashlytics/Crashlytics/Models/FIRCLSSettings.h"
2325#import " Crashlytics/Crashlytics/Private/FIRCrashlyticsReport_Private.h"
2426#import " Crashlytics/Crashlytics/Public/FirebaseCrashlytics/FIRCrashlyticsReport.h"
2527
@@ -31,6 +33,8 @@ @interface FIRCLSExistingReportManager ()
3133@property (nonatomic , strong ) FIRCLSFileManager *fileManager;
3234@property (nonatomic , strong ) FIRCLSReportUploader *reportUploader;
3335@property (nonatomic , strong ) NSOperationQueue *operationQueue;
36+ @property (nonatomic , strong ) FIRCLSSettings *settings;
37+ @property (nonatomic , strong ) FIRCLSDataCollectionArbiter *dataArbiter;
3438
3539// This list of active reports excludes the brand new active report that will be created this run of
3640// the app.
@@ -52,7 +56,9 @@ - (instancetype)initWithManagerData:(FIRCLSManagerData *)managerData
5256 }
5357
5458 _fileManager = managerData.fileManager ;
59+ _settings = managerData.settings ;
5560 _operationQueue = managerData.operationQueue ;
61+ _dataArbiter = managerData.dataArbiter ;
5662 _reportUploader = reportUploader;
5763
5864 return self;
@@ -91,16 +97,36 @@ - (NSUInteger)unsentReportsCount {
9197 */
9298- (NSArray <NSString *> *)getUnsentActiveReportsAndDeleteEmptyOrOld : (NSArray *)reportPaths {
9399 NSMutableArray <FIRCLSInternalReport *> *validReports = [NSMutableArray array ];
100+ NSMutableArray <FIRCLSInternalReport *> *reports = [NSMutableArray array ];
101+
94102 for (NSString *path in reportPaths) {
95103 FIRCLSInternalReport *_Nullable report = [FIRCLSInternalReport reportWithPath: path];
96104 if (!report) {
97105 continue ;
98106 }
99107
108+ [reports addObject: report];
109+ }
110+
111+ if (reports.count == 0 ) {
112+ return @[];
113+ }
114+
115+ [reports sortUsingFunction: compareNewer context: nil ];
116+ NSString *newestReportPath = [reports firstObject ].path ;
117+
118+ // If there was a MetricKit event recorded on the last run of the app, add it to the newest
119+ // report.
120+ if (self.settings .metricKitCollectionEnabled &&
121+ [self .fileManager metricKitDiagnosticFileExists ]) {
122+ [self .fileManager createEmptyMetricKitFile: newestReportPath];
123+ }
124+
125+ for (FIRCLSInternalReport *report in reports) {
100126 // Delete reports without any crashes or non-fatals
101127 if (![report hasAnyEvents ]) {
102128 [self .operationQueue addOperationWithBlock: ^{
103- [self .fileManager removeItemAtPath: path];
129+ [self .fileManager removeItemAtPath: report. path];
104130 }];
105131 continue ;
106132 }
0 commit comments