Skip to main content
Version: 5.11.8

Synchronous/Asynchronous/SendQueue

BACKND provides three ways of using methods.

  • Synchronous
  • Asynchronous
  • SendQueue

Each type of method has its own pros and cons; therefore, it is recommended to use the one suitable for the situation, rather than choosing just one.

Synchronous type

This method provides the result at the site of the request no matter how long it takes from the time of the request.
When this method is called, the game may look like it has stopped because the main thread is stopped until the return value is returned.

Unlike asynchronous methods, this method is called sequentially, so there is no chance of it getting out of order, and there is no chance of errors being thrown by other threads during operation, giving reliable results.

When to use

It is recommended to use this when calling functions that must be carried out properly.

  • Login functions, such as login and token renewal
  • Functions related to charging, such as receipt verification and provision of paid items

Example

//Load user information(synchronous)
Backend.BMember.GetUserInfo();
// Post-action

Asynchronous type

Unlike synchronous methods, this is a method that does not wait for the execution result at the time of the call.

In the case of asynchronous methods, the thread that called the asynchronous method can perform the next task without waiting for the result of that method call.
If you perform tasks during this time, such as loading, you may let the user feel the data is being loaded or saved instead of experiencing the program's suspension.

If multiple asynchronous methods are called nearly at the same time(for loop, etc.), the request may take an unusual amount of time under the following conditions.

  • If the requests to the server were pending according to the OS scheduler and all of them are sent to the server at once, the requests may take an unusual amount of time.
  • If there are more asynchronous requests called at once than the number of asynchronous IO threads currently being used by the client, the requests may take an unusual amount of time while generating a new IO thread.

There are two ways to execute the callback method of asynchronous methods.

1. How to execute in asynchronous IO thread

According to Unity's policy, it is impossible to access the Unity MonoBehaviour object from another thread at this time.
In short, as you cannot access Unity objects, UI objects, etc. in the callback method, you may use an additional Dispatcher for processing according to the request result of the asynchronous method.

2. How to execute in the main thread

You can access the Unity MonoBehavior object in the callback method by using the callback method pooling function.
In short, you can access Unity objects, UI objects, etc. within the callback method.
For more information on the callback method pooling function, please refer to the relevant documentation.

When to use

In SDK versions lower than 5.3.0, if you call four or more asynchronous methods at once, request times may be unusually long.
Developers using SDK versions lower than 5.3.0 are recommended to update to SDK version 5.3.0 or later.

It is recommended to use this when calling methods in most situations.

  • Currently, it is recommended to call no more than eight methods
  • When there is no need to call methods sequentially
  • When the current data is removed upon closing the game

Example

//Load user information(asynchronous)
Backend.BMember.GetUserInfo(callback =>
{
// Post-action
});

SendQueue type

This type stacks methods in a queue and calls them sequentially, not immediately.
It combines the advantage of synchronous method(calling in order) and that of asynchronous method(the next task is performed without waiting for the method result) calling. In addition, the program does not appear to have stopped.

Methods in SendQueue are called, and the next method is called after a response is received.
As a result, methods are called sequentially but the calling of a method may be delayed if there are many requests inserted into SendQueue.

The callback method of the method stacked in SendQueue is designed to be operated in the main thread of Unity.
Therefore, unlike asynchronous methods, you can access Unity objects and UI objects within the callback method.

When to use

It is recommended to use this when calling methods in most situations.

  • When the methods should be called sequentially

Example

//Load user information(SendQueue)
SendQueue.Enqueue(Backend.BMember.GetUserInfo, callback =>
{
// Post-action
});