iOS SDK Status
A common SDK task is to write data to the Acoustic mobile service upon app startup. However, you should never attempt to call SDK APIs unless you are sure the SDK is running. If the SDK is not running, a backtrace can result. This backtrace will generally not impact your user experience nor impact the SDK later starting properly, but it can be concerning to see these backtraces in your error logging tool.
To assist with ensuring the SDK is running, the SDK offers two public methods in version 3.8.7 and later:
- sdkState
- sdkStateIsRunning
SDK states
There are four distinct states the SDK can be in:
Not Initialized
The SDK's initial (cold) state. The state of the SDK before the manual init MCEsdk.shared.handleApplicationLaunch
method is called or before the automatic init has started. This state is the default state for the SDK.
Initializing
The SDK state once the initialization of the SDK has been started, but still needs to finish initializing and registering a channelId
and userId
. Once the initialization process completes, the SDK will either go to the Running state if all initialization and registration succeed or to the Stopped state if something in the process fails.
Running
The running state means that the SDK is ready to be used. The userId
and channelId
are successfully registered, and all public methods of the SDK are ready to be used.
Stopped
The stopped state means that something went wrong in the registration or initialization process, and the SDK is not ready to be used.
Public methods
sdkState
This method will return the SDK's current state (Not Initialized, Initializing, Running, or Stopped). You need to call this method each time the SDK state is needed.
switch MCESdk.shared.sdkState() {
case MCESdkState.Initializing:
print("SDK is initializing")
case MCESdkState.Running:
print("SDK is running")
case MCESdkState.Stopped:
print("SDK is stopped")
case MCESdkState.NotInitialized:
print("SDK is Not Initialized")
default: break
}
switch ([self sdkState]){
case Initializing:
NSLog(@"SDK is Initializing");
case Running:
NSLog(@"SDK is running");
case Stopped:
NSLog(@"SDK is Stopped");
case NotInitialized:
NSLog(@"SDK is NotInitialized");
default: break;
}
sdkStateIsRunning
This method can be called when you need to use an SDK API, but you need to know whether the SDK has completed the startup and is ready. The method will notify you when the state of the SDK is set to Running or has Stopped due to errors. The method allows you to add code inside the completion handler that waits for the SDK to be in the Running state before it runs.
Note:
If you are using the manual initialization process, make sure that
handleApplicationLaunch
is called at some point during the application life cycle.
MCESdk.shared.sdkStateIsRunning { (error) in
if error != nil {
print(error.localizedDescription)
return }
// Successfully running the sdk here...
}
[self sdkStateIsRunning:^(NSError * _Nullable error) {
if(error != nil){
NSLog(error.localizedDescription);
return; }
// Successfully running the sdk here...
}];
Updated almost 2 years ago