@@ -111,6 +111,8 @@ @interface FIRCrashlytics () <FIRLibrary,
111111// Dependencies common to each of the Controllers
112112@property (nonatomic , strong ) FIRCLSManagerData *managerData;
113113
114+ @property (nonatomic , nullable ) FBLPromise *contextInitPromise;
115+
114116@end
115117
116118@implementation FIRCrashlytics
@@ -197,14 +199,15 @@ - (instancetype)initWithApp:(FIRApp *)app
197199 });
198200 }
199201
200- [[[_reportManager startWithProfiling ] then: ^id _Nullable (NSNumber *_Nullable value) {
201- if (![value boolValue ]) {
202- FIRCLSErrorLog (@" Crash reporting could not be initialized" );
203- }
204- return value;
205- }] catch: ^void (NSError *error) {
206- FIRCLSErrorLog (@" Crash reporting failed to initialize with error: %@ " , error);
207- }];
202+ _contextInitPromise =
203+ [[[_reportManager startWithProfiling ] then: ^id _Nullable (NSNumber *_Nullable value) {
204+ if (![value boolValue ]) {
205+ FIRCLSErrorLog (@" Crash reporting could not be initialized" );
206+ }
207+ return value;
208+ }] catch: ^void (NSError *error) {
209+ FIRCLSErrorLog (@" Crash reporting failed to initialize with error: %@ " , error);
210+ }];
208211
209212 // RemoteConfig subscription should be made after session report directory created.
210213 if (remoteConfig) {
@@ -307,7 +310,14 @@ - (void)processDidCrashDuringPreviousExecution {
307310
308311#pragma mark - API: Logging
309312- (void )log : (NSString *)msg {
310- FIRCLSLog (@" %@ " , msg);
313+ if (!_contextInitPromise) {
314+ FIRCLSErrorLog (@" Context has not been inialized when log message: %@ " , msg);
315+ return ;
316+ }
317+ [_contextInitPromise then: ^id _Nullable (id _Nullable value) {
318+ FIRCLSLog (@" %@ " , msg);
319+ return nil ;
320+ }];
311321}
312322
313323- (void )logWithFormat : (NSString *)format , ... {
@@ -350,17 +360,27 @@ - (void)deleteUnsentReports {
350360
351361#pragma mark - API: setUserID
352362- (void )setUserID : (nullable NSString *)userID {
353- FIRCLSUserLoggingRecordInternalKeyValue (FIRCLSUserIdentifierKey, userID);
363+ [self waitForContextInit: userID
364+ callback: ^{
365+ FIRCLSUserLoggingRecordInternalKeyValue (FIRCLSUserIdentifierKey, userID);
366+ }];
354367}
355368
356369#pragma mark - API: setCustomValue
357370
358371- (void )setCustomValue : (nullable id )value forKey : (NSString *)key {
359- FIRCLSUserLoggingRecordUserKeyValue (key, value);
372+ NSString *contextLog = [NSString stringWithFormat: @" \n key: %@ ,\n value: %@ " , key, value];
373+ [self waitForContextInit: contextLog
374+ callback: ^{
375+ FIRCLSUserLoggingRecordUserKeyValue (key, value);
376+ }];
360377}
361378
362379- (void )setCustomKeysAndValues : (NSDictionary *)keysAndValues {
363- FIRCLSUserLoggingRecordUserKeysAndValues (keysAndValues);
380+ [self waitForContextInit: keysAndValues.description
381+ callback: ^{
382+ FIRCLSUserLoggingRecordUserKeysAndValues (keysAndValues);
383+ }];
364384}
365385
366386#pragma mark - API: Development Platform
@@ -383,8 +403,11 @@ - (NSString *)developmentPlatformName {
383403}
384404
385405- (void )setDevelopmentPlatformName : (NSString *)developmentPlatformName {
386- FIRCLSUserLoggingRecordInternalKeyValue (FIRCLSDevelopmentPlatformNameKey,
387- developmentPlatformName);
406+ [self waitForContextInit: developmentPlatformName
407+ callback: ^{
408+ FIRCLSUserLoggingRecordInternalKeyValue (FIRCLSDevelopmentPlatformNameKey,
409+ developmentPlatformName);
410+ }];
388411}
389412
390413- (NSString *)developmentPlatformVersion {
@@ -393,8 +416,11 @@ - (NSString *)developmentPlatformVersion {
393416}
394417
395418- (void )setDevelopmentPlatformVersion : (NSString *)developmentPlatformVersion {
396- FIRCLSUserLoggingRecordInternalKeyValue (FIRCLSDevelopmentPlatformVersionKey,
397- developmentPlatformVersion);
419+ [self waitForContextInit: developmentPlatformVersion
420+ callback: ^{
421+ FIRCLSUserLoggingRecordInternalKeyValue (FIRCLSDevelopmentPlatformVersionKey,
422+ developmentPlatformVersion);
423+ }];
398424}
399425
400426#pragma mark - API: Errors and Exceptions
@@ -403,20 +429,34 @@ - (void)recordError:(NSError *)error {
403429}
404430
405431- (void )recordError : (NSError *)error userInfo : (NSDictionary <NSString *, id> *)userInfo {
406- NSString *rolloutsInfoJSON = [_remoteConfigManager getRolloutAssignmentsEncodedJsonString ];
407- FIRCLSUserLoggingRecordError (error, userInfo, rolloutsInfoJSON);
432+ NSString *contextLog = [NSString
433+ stringWithFormat: @" \n error: %@ ,\n userInfo: %@ " , error.description, userInfo.description];
434+ [self waitForContextInit: contextLog
435+ callback: ^{
436+ NSString *rolloutsInfoJSON =
437+ [_remoteConfigManager getRolloutAssignmentsEncodedJsonString ];
438+ FIRCLSUserLoggingRecordError (error, userInfo, rolloutsInfoJSON);
439+ }];
408440}
409441
410442- (void )recordExceptionModel : (FIRExceptionModel *)exceptionModel {
411- NSString *rolloutsInfoJSON = [_remoteConfigManager getRolloutAssignmentsEncodedJsonString ];
412- FIRCLSExceptionRecordModel (exceptionModel, rolloutsInfoJSON);
443+ [self waitForContextInit: exceptionModel.description
444+ callback: ^{
445+ NSString *rolloutsInfoJSON =
446+ [_remoteConfigManager getRolloutAssignmentsEncodedJsonString ];
447+ FIRCLSExceptionRecordModel (exceptionModel, rolloutsInfoJSON);
448+ }];
413449}
414450
415451- (void )recordOnDemandExceptionModel : (FIRExceptionModel *)exceptionModel {
416- [self .managerData.onDemandModel
417- recordOnDemandExceptionIfQuota: exceptionModel
418- withDataCollectionEnabled: [self .dataArbiter isCrashlyticsCollectionEnabled ]
419- usingExistingReportManager: self .existingReportManager];
452+ [self waitForContextInit: exceptionModel.description
453+ callback: ^{
454+ [self .managerData.onDemandModel
455+ recordOnDemandExceptionIfQuota: exceptionModel
456+ withDataCollectionEnabled: [self .dataArbiter
457+ isCrashlyticsCollectionEnabled ]
458+ usingExistingReportManager: self .existingReportManager];
459+ }];
420460}
421461
422462#pragma mark - FIRSessionsSubscriber
@@ -445,4 +485,16 @@ - (void)rolloutsStateDidChange:(FIRRolloutsState *_Nonnull)rolloutsState {
445485 [_remoteConfigManager updateRolloutsStateWithRolloutsState: rolloutsState
446486 reportID: currentReportID];
447487}
488+
489+ #pragma mark - Private Helpsers
490+ - (void )waitForContextInit : (NSString *)contextLog callback : (void (^)(void ))callback {
491+ if (!_contextInitPromise) {
492+ FIRCLSErrorLog (@" Crashlytics method called before SDK was initialized: %@ " , contextLog);
493+ return ;
494+ }
495+ [_contextInitPromise then: ^id _Nullable (id _Nullable value) {
496+ callback ();
497+ return nil ;
498+ }];
499+ }
448500@end
0 commit comments