Skip to main content
Version: SDK-5.11.2

Calling Handler From Unity's Main Thread

Description

The common error handler is called asynchronously by default.
Therefore, an error will occur upon calling elements, including UI components that can only be accessed via the main thread.

 Backend.ErrorHandler.OnMaintenanceError = () => {
gameObject.GetComponentInChildren<Text>().text = "Under maintenance.";
};

OnMaintenanceError Exception : UnityEngine.UnityException: get_gameObject can only be called from the main thread.

To access the UI within the handler, call additional methods as shown in the following example:

Example

public class BackendErrorManager : MonoBehaviour {

void Start() {

if(Backend.IsInitialized) {
Backend.ErrorHandler.InitializePoll(true);

Backend.ErrorHandler.OnMaintenanceError = () => {
Debug.Log("Maintenance error occurred!!!");
gameObject.GetComponentInChildren<Text>().text = "Under maintenance.";
};
Backend.ErrorHandler.OnTooManyRequestError = () => {
Debug.Log("403 error occurred!!!");
gameObject.GetComponentInChildren<Text>().text = "Excessive requests detected.";
};
Backend.ErrorHandler.OnTooManyRequestByLocalError = () => {
Debug.Log("403 local error occurred!!!");
gameObject.GetComponentInChildren<Text>().text = "Excessive requests.";
};
Backend.ErrorHandler.OnOtherDeviceLoginDetectedError = () => {
Debug.Log("Cannot refresh!!!");
gameObject.GetComponentInChildren<Text>().text = "Login detected from another device.";
};
}
}

void Update() {
Backend.ErrorHandler.Poll();
}
}

1. Initialize polling process - InitializePoll()

public void InitializePoll(bool useAsyncPoll);

Parameter

ValueTypeDescription
useAsyncPollboolActivation status of main thread handling

Description

The common error handler's way of handling is configured to be enabled in Unity's main thread.
It is recommended you activate the logic that allows access to the UI and other elements within the handler should it exist.

When useAsyncPoll is 'true,' all common server handlers that have been registered are executed in the main thread.
When useAsyncPoll is 'false' or not called, all common server handlers that have been registered are executed in the external thread.

Example

void Start() {
if(Backend.IsInitialized) {
Backend.ErrorHandler.InitializePoll(true);
}

2. Poll

When the error relevant to the common error handler is called after the handler's registration, it is stored in a separate queue for the call to be made from the asynchronous method thread to the main thread.
To execute the handler stored in the queue, the following polling method must be called regularly.

void Update()  {
Backend.ErrorHandler.Poll();
}

The following is recommended to call the polling method regularly:

  • Declare the polling method in Unity object of the Update method
  • Generate a coroutine that is called regularly and declare the polling method within that coroutine
  • Create a separate method that is called regularly and declare the polling method within that method

The following is not recommended to call the polling method regularly:

  • Declare a recursive method, and declare the polling method within that recursive method