Search Unity

Other Use UDP Initialisation in a async task not initialising

Discussion in 'Unity Distribution Portal (UDP)' started by ManuelRauber, Apr 13, 2022.

  1. ManuelRauber

    ManuelRauber

    Joined:
    Apr 3, 2015
    Posts:
    122
    Hi,

    in my project I use UniTask to streamline async stuff in Unity. I had a basic UDP integration that worked with the Qoo App Store.

    In case you're not familiar with UniTask, it explains here that all async/await is run on the Unity main thread: https://github.com/Cysharp/UniTask#basics-of-unitask-and-asyncoperation (except if you use it's method SwitchToThreadPool which I do not use).

    Then I wanted to do some deeper integration with Qoo App Store, which is also quite Callback-heavy. In that case, I normally start using UniTask to get a nice async/await-able API for 3rd party code.

    To start, I simply changed how UDP is initialized from a callback approach to a task approach, like this:

    Code (CSharp):
    1. public static class UdpIntegration
    2. {
    3.   private class GameStoreUdpListener : IInitListener
    4.   {
    5.     private readonly UniTaskCompletionSource<bool> _taskCompletionSource;
    6.  
    7.     public GameStoreUdpListener(UniTaskCompletionSource<bool> taskCompletionSource)
    8.     {
    9.       _taskCompletionSource = taskCompletionSource;
    10.     }
    11.  
    12.     public void OnInitialized(UserInfo userInfo)
    13.     {
    14.       Debug.Log($"UDP Successfully initialized for {userInfo.Channel}");
    15.       _taskCompletionSource.TrySetResult(true);
    16.     }
    17.  
    18.     public void OnInitializeFailed(string message)
    19.     {
    20.       Debug.LogError($"UDP initialization failed: {message}");
    21.       _taskCompletionSource.TrySetResult(false);
    22.     }
    23.   }
    24.  
    25.   public static UniTask<UdpInitializationResult> InitializeAsync()
    26.   {
    27.     Debug.Log("Starting UDP Initialization");
    28.  
    29.     var taskCompletionSource = new UniTaskCompletionSource();
    30.  
    31.     StoreService.Initialize(new GameStoreUdpListener(taskCompletionSource));
    32.  
    33.     return taskCompletionSource.Task;
    34.   }
    35. }
    In my start up behaviour I use it like this:

    Code (CSharp):
    1. [UsedImplicitly]
    2. private async UniTaskVoid Start()
    3. {
    4.   // Other async/await init stuff
    5.  
    6. #if UNITY_UDP
    7.   var initializationResult = await UdpIntegration.InitializeAsync();
    8. #endif
    9.  
    10.   // Other async/await init stuff
    11. }
    When I take a look at Android Logcat during the startup, I notice, that I get the message Starting UDP Initialization and then nothing. No other log message shows up and the init of the app hangs, which makes sense, because taskCompletionSource is never completed, because the IInitListener callbacks are not called.

    Shouldn't it be possible to use it that way? Or could there be anything else that prevents calling the callbacks?

    Thanks!

    Edit:

    * Latest 2021.2
    * Latest UDP Package
     
    Last edited: Apr 13, 2022
  2. ManuelRauber

    ManuelRauber

    Joined:
    Apr 3, 2015
    Posts:
    122