@@ -111,6 +111,8 @@ @interface FIRCrashlytics () <FIRLibrary,
111
111
// Dependencies common to each of the Controllers
112
112
@property (nonatomic , strong ) FIRCLSManagerData *managerData;
113
113
114
+ @property (nonatomic , nullable ) FBLPromise *contextInitPromise;
115
+
114
116
@end
115
117
116
118
@implementation FIRCrashlytics
@@ -197,14 +199,15 @@ - (instancetype)initWithApp:(FIRApp *)app
197
199
});
198
200
}
199
201
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
+ }];
208
211
209
212
// RemoteConfig subscription should be made after session report directory created.
210
213
if (remoteConfig) {
@@ -307,7 +310,14 @@ - (void)processDidCrashDuringPreviousExecution {
307
310
308
311
#pragma mark - API: Logging
309
312
- (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
+ }];
311
321
}
312
322
313
323
- (void )logWithFormat : (NSString *)format , ... {
@@ -350,17 +360,27 @@ - (void)deleteUnsentReports {
350
360
351
361
#pragma mark - API: setUserID
352
362
- (void )setUserID : (nullable NSString *)userID {
353
- FIRCLSUserLoggingRecordInternalKeyValue (FIRCLSUserIdentifierKey, userID);
363
+ [self waitForContextInit: userID
364
+ callback: ^{
365
+ FIRCLSUserLoggingRecordInternalKeyValue (FIRCLSUserIdentifierKey, userID);
366
+ }];
354
367
}
355
368
356
369
#pragma mark - API: setCustomValue
357
370
358
371
- (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
+ }];
360
377
}
361
378
362
379
- (void )setCustomKeysAndValues : (NSDictionary *)keysAndValues {
363
- FIRCLSUserLoggingRecordUserKeysAndValues (keysAndValues);
380
+ [self waitForContextInit: keysAndValues.description
381
+ callback: ^{
382
+ FIRCLSUserLoggingRecordUserKeysAndValues (keysAndValues);
383
+ }];
364
384
}
365
385
366
386
#pragma mark - API: Development Platform
@@ -383,8 +403,11 @@ - (NSString *)developmentPlatformName {
383
403
}
384
404
385
405
- (void )setDevelopmentPlatformName : (NSString *)developmentPlatformName {
386
- FIRCLSUserLoggingRecordInternalKeyValue (FIRCLSDevelopmentPlatformNameKey,
387
- developmentPlatformName);
406
+ [self waitForContextInit: developmentPlatformName
407
+ callback: ^{
408
+ FIRCLSUserLoggingRecordInternalKeyValue (FIRCLSDevelopmentPlatformNameKey,
409
+ developmentPlatformName);
410
+ }];
388
411
}
389
412
390
413
- (NSString *)developmentPlatformVersion {
@@ -393,8 +416,11 @@ - (NSString *)developmentPlatformVersion {
393
416
}
394
417
395
418
- (void )setDevelopmentPlatformVersion : (NSString *)developmentPlatformVersion {
396
- FIRCLSUserLoggingRecordInternalKeyValue (FIRCLSDevelopmentPlatformVersionKey,
397
- developmentPlatformVersion);
419
+ [self waitForContextInit: developmentPlatformVersion
420
+ callback: ^{
421
+ FIRCLSUserLoggingRecordInternalKeyValue (FIRCLSDevelopmentPlatformVersionKey,
422
+ developmentPlatformVersion);
423
+ }];
398
424
}
399
425
400
426
#pragma mark - API: Errors and Exceptions
@@ -403,20 +429,34 @@ - (void)recordError:(NSError *)error {
403
429
}
404
430
405
431
- (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
+ }];
408
440
}
409
441
410
442
- (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
+ }];
413
449
}
414
450
415
451
- (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
+ }];
420
460
}
421
461
422
462
#pragma mark - FIRSessionsSubscriber
@@ -445,4 +485,16 @@ - (void)rolloutsStateDidChange:(FIRRolloutsState *_Nonnull)rolloutsState {
445
485
[_remoteConfigManager updateRolloutsStateWithRolloutsState: rolloutsState
446
486
reportID: currentReportID];
447
487
}
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
+ }
448
500
@end
0 commit comments