Unlike synchronous methods, asynchronous methods do not wait for the execution result at the time of the call.
- Synchronous method
When calling a synchronous method, the operation of the thread that called the method will practically stop until the result of the call is provided.
In this case, it may appear as if the program has stopped for the user.
- Asynchronous method
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.
Precautions when using asynchronous methods
For both synchronous methods and asynchronous methods, if you call a method to send a request to the server, it is not sent to the server immediately.
All requests are sent to the server when the OS can process them based on the OS scheduler.
However, it appears to have been sent to the server immediately after the method is called because the required time is very short.
For asynchronous methods, multiple asynchronous methods can be called at once because their requests and responses are processed in the asynchronous IO thread.
In such a case, you must pay attention to the following matters:
- The order of methods may not be in accordance with their call order.
- If you perform a task like modifying the same row of game information, some requests may fail because the final reading consistency is not ensured.
The callback method of asynchronous methods is executed in the asynchronous IO thread.
According to Unity's policy, the callback method executed in a separate thread cannot access the Unity MonoBehaviour object 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.
Callback method pooling
For a callback method, it cannot access Unity's MonoBehavior object because it is executed in the asynchronous IO thread as explained above.
At this time, the callback method can access Unity's MonoBehavior object if it is executed in the main thread using callback method pooling. For more information on how to use callback method pooling, please refer to the Callback Method Pooling documentation.
Situations where asynchronous methods are called
- It is recommended for most situations.
- When requesting multiple asynchronous methods at once, it is recommended to request no more than eight.
- When the order of the method call does not need to be ensured
- When the data of the current user must be stored when closing the game
Situations where asynchronous methods should not be called
- When multiple asynchronous methods must be requested at once and the order of the request must be ensured
- When multiple asynchronous methods must be requested at once while modifying the same data
- When modifying game information, only one request will succeed if you send multiple requests to modify the same row in a table at once.
- The same applies to other tasks, including modification of guild metadata/goods and real-time update of rankings.