Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Check if data exist

Discussion in 'Cloud Save' started by mvdevp, Dec 11, 2022.

  1. mvdevp

    mvdevp

    Joined:
    Aug 15, 2021
    Posts:
    3
    what is the correct way to check if a key exists or not.

    Thats how im doing it atm but i want to know if exist some better way of do that, because im using the catch excepction as a else...

    Thanks


    Code (CSharp):
    1.   try
    2.             {
    3.                 var d = await CloudSaveService.Instance.Data.LoadAsync(new HashSet<string> { "HEAD" }) ;
    4.                 if (d.Values.Count > 0)
    5.                 {
    6.                     SceneManager.LoadScene(1);
    7.                 }
    8.             }
    9.             catch (KeyNotFoundException e)
    10.             {
    11.                 Debug.Log(e);
    12.              
    13.                    
     
  2. MileyUnity

    MileyUnity

    Joined:
    Jan 3, 2020
    Posts:
    92
    The if check inside there is the right way to check if the key doesn't yet exist, a different way would be to load all the data through LoadAllAsync and simply check the if the resulting dictionary contains the key you're looking for.

    The try catch block is for if there are any networking or service issues with CloudSave like timeouts, no internet connection, the service being unavailable or other kinds of networking issues. These are handled through a CloudSaveException object, the KeyNotFoundException that you've put here does not exist. The exceptions that do exist can be found here for the individual functions on the ICloudSaveDataClient object
     
  3. mvdevp

    mvdevp

    Joined:
    Aug 15, 2021
    Posts:
    3
    Thanks
     
  4. gugjuiu19

    gugjuiu19

    Joined:
    Jul 15, 2021
    Posts:
    5
    you mean some think like this
    Code (CSharp):
    1.         try
    2.         {
    3.             Dictionary<string, string> savedData = await CloudSaveService.Instance.Data.LoadAsync(new HashSet<string> { "name" });
    4.             Debug.Log("Has Name");
    5.             PhotonNetwork.NickName = savedData["name"];
    6.             InstanceData.Instance.BombCoinCurrencyDefinition = await EconomyService.Instance.Configuration.GetCurrencyAsync(InstanceData.Instance.currencyID);
    7.             InstanceData.Instance.playersGoldBarBalance = await InstanceData.Instance.BombCoinCurrencyDefinition.GetPlayerBalanceAsync();
    8.             Debug.Log(InstanceData.Instance.playersGoldBarBalance.Balance);
    9.             InstanceData.Instance.SetUpActions();
    10.             InstanceData.Instance.definitions = await EconomyService.Instance.Configuration.GetInventoryItemsAsync();
    11.             foreach (InventoryItemDefinition inventoryItem in InstanceData.Instance.definitions)
    12.             {
    13.                 Debug.Log(inventoryItem.Name);
    14.                 Debug.Log(inventoryItem.CustomData["type"].ToString());
    15.             }
    16.             InstanceData.Instance.PlayerName = PhotonNetwork.NickName;
    17.             Transtion.Instance.StartCoroutine("TransformFXShow", "Home");
    18.         }
    19.         catch(Exception ex)
    20.         {
    21.             Debug.Log("New Player");
    22.             InputName.active = true;
    23.         }
     
  5. MileyUnity

    MileyUnity

    Joined:
    Jan 3, 2020
    Posts:
    92
    Technically no, the savedData dictionary on line 3 can be empty if there wasn't any data for that key. The request still execute successfully but would simply returned an empty response. Other than that I don't see any issues that stand out