Search Unity

Bug PlayerPrefs don't work when using Firebase

Discussion in 'Scripting' started by danielesuppo, Apr 20, 2022.

  1. danielesuppo

    danielesuppo

    Joined:
    Oct 20, 2015
    Posts:
    331
    Hello all, I think that this is a bug:
    using Firebase, after checking for dependecies, when I try to save/load data with PlayerPrefs the code (not Unity) get stuck on the PlayerPrefs methods...
    Consider this pseudo code:
    Code (CSharp):
    1. private void Awake() {
    2. FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
    3.         {
    4.             var dependencyStatus = task.Result;
    5.             if (dependencyStatus == Firebase.DependencyStatus.Available)
    6.             {
    7.                 Debug.Log("Firebase is available");
    8.                 Init();
    9.              }
    10.             else
    11.             {
    12.                 Debug.Log("Firebase is NOT available");
    13.             }
    14.         });
    15.     }
    16.  
    17.     void Init(){
    18.         print("I'm reading the PlayerPrefs value..."); // here the code get stuck, when called from FirebaseApp.CheckAndFixDependenciesAsync
    19.         Debug.Log("Show my string: " + PlayerPrefs.GetString("someKey"));
    20.         print("I've finished to read the PlayerPrefs value!");
    21.     }
    You will see that "Show my string" will never print, and no errors are displayed in the console.
    And the most weird thing is that any other classes will not be able to use any PlayerPrefs methods, nor in reading not in writing, because them will get stuck in the same way.

    Any help to fix this bug would be much appreciated!
    I'm using Unity 2021.2.10f1
     
    Last edited: Apr 20, 2022
  2. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    try using ContinueWithOnMainThread instead of ContinueWith, or just set a flag and check on it later to ensure the logic you need in unity is running on the correct thread
     
    danielesuppo and Kurt-Dekker like this.
  3. danielesuppo

    danielesuppo

    Joined:
    Oct 20, 2015
    Posts:
    331
    Many thanks! Using "ContinueWithOnMainThread" solved the problem
     
    passerbycmc likes this.