Bläddra i källkod

Adding development platform setter APIs to context init promise chain (#15356)

themiswang 6 månader sedan
förälder
incheckning
ba8674d887

+ 3 - 0
Crashlytics/CHANGELOG.md

@@ -1,3 +1,6 @@
+# Unreleased
+- [fixed] Make set development platform APIs to chain on Crashlytics context init promise.
+
 # 12.3.0
 - [fixed] Add missing nanopb dependency to fix SwiftPM builds when building
   dynamically linked libraries. (#15276)

+ 33 - 12
Crashlytics/Crashlytics/FIRCrashlytics.m

@@ -111,6 +111,8 @@ NSString *const FIRCLSGoogleTransportMappingID = @"1206";
 // Dependencies common to each of the Controllers
 @property(nonatomic, strong) FIRCLSManagerData *managerData;
 
+@property(nonatomic, nullable) FBLPromise *contextInitPromise;
+
 @end
 
 @implementation FIRCrashlytics
@@ -197,14 +199,15 @@ NSString *const FIRCLSGoogleTransportMappingID = @"1206";
       });
     }
 
-    [[[_reportManager startWithProfiling] then:^id _Nullable(NSNumber *_Nullable value) {
-      if (![value boolValue]) {
-        FIRCLSErrorLog(@"Crash reporting could not be initialized");
-      }
-      return value;
-    }] catch:^void(NSError *error) {
-      FIRCLSErrorLog(@"Crash reporting failed to initialize with error: %@", error);
-    }];
+    _contextInitPromise =
+        [[[_reportManager startWithProfiling] then:^id _Nullable(NSNumber *_Nullable value) {
+          if (![value boolValue]) {
+            FIRCLSErrorLog(@"Crash reporting could not be initialized");
+          }
+          return value;
+        }] catch:^void(NSError *error) {
+          FIRCLSErrorLog(@"Crash reporting failed to initialize with error: %@", error);
+        }];
 
     // RemoteConfig subscription should be made after session report directory created.
     if (remoteConfig) {
@@ -383,8 +386,11 @@ NSString *const FIRCLSGoogleTransportMappingID = @"1206";
 }
 
 - (void)setDevelopmentPlatformName:(NSString *)developmentPlatformName {
-  FIRCLSUserLoggingRecordInternalKeyValue(FIRCLSDevelopmentPlatformNameKey,
-                                          developmentPlatformName);
+  [self waitForContextInit:developmentPlatformName
+                  callback:^{
+                    FIRCLSUserLoggingRecordInternalKeyValue(FIRCLSDevelopmentPlatformNameKey,
+                                                            developmentPlatformName);
+                  }];
 }
 
 - (NSString *)developmentPlatformVersion {
@@ -393,8 +399,11 @@ NSString *const FIRCLSGoogleTransportMappingID = @"1206";
 }
 
 - (void)setDevelopmentPlatformVersion:(NSString *)developmentPlatformVersion {
-  FIRCLSUserLoggingRecordInternalKeyValue(FIRCLSDevelopmentPlatformVersionKey,
-                                          developmentPlatformVersion);
+  [self waitForContextInit:developmentPlatformVersion
+                  callback:^{
+                    FIRCLSUserLoggingRecordInternalKeyValue(FIRCLSDevelopmentPlatformVersionKey,
+                                                            developmentPlatformVersion);
+                  }];
 }
 
 #pragma mark - API: Errors and Exceptions
@@ -445,4 +454,16 @@ NSString *const FIRCLSGoogleTransportMappingID = @"1206";
   [_remoteConfigManager updateRolloutsStateWithRolloutsState:rolloutsState
                                                     reportID:currentReportID];
 }
+
+#pragma mark - Private Helpsers
+- (void)waitForContextInit:(NSString *)contextLog callback:(void (^)(void))callback {
+  if (!_contextInitPromise) {
+    FIRCLSErrorLog(@"Crashlytics method called before SDK was initialized: %@", contextLog);
+    return;
+  }
+  [_contextInitPromise then:^id _Nullable(id _Nullable value) {
+    callback();
+    return nil;
+  }];
+}
 @end

+ 23 - 0
Crashlytics/UnitTests/FIRCLSContextManagerTests.m

@@ -122,4 +122,27 @@ NSString *const TestContextSessionID2 = @"TestContextSessionID2";
   XCTAssertEqualObjects(adapter.identity.app_quality_session_id, TestContextSessionID2);
 }
 
+// This test is for chain on init promise for development platform related setters
+- (void)test_promisesChainOnInitPromiseInOrder {
+  NSMutableArray<NSString *> *result = @[].mutableCopy;
+  NSMutableArray<NSString *> *expectation = @[].mutableCopy;
+
+  for (int j = 0; j < 100; j++) {
+    [expectation addObject:[NSString stringWithFormat:@"%d", j]];
+  }
+
+  FBLPromise *promise = [self.contextManager setupContextWithReport:self.report
+                                                           settings:self.mockSettings
+                                                        fileManager:self.fileManager];
+
+  for (int i = 0; i < 100; i++) {
+    [promise then:^id _Nullable(id _Nullable value) {
+      [result addObject:[NSString stringWithFormat:@"%d", i]];
+      if (i == 99) {
+        XCTAssertTrue([result isEqualToArray:expectation]);
+      }
+      return nil;
+    }];
+  }
+}
 @end