简体   繁体   English

从 Firebase 数据库中检索数据并统一保存数据

[英]Issue retrieving data from Firebase Database and saving data on unity

I am using the following code to retrieve data from Firebase database on a user with Unity3D, in our case i am getting User Level:我正在使用以下代码从具有 Unity3D 的用户的 Firebase 数据库中检索数据,在我们的例子中,我获得了用户级别:

 FirebaseDatabase.DefaultInstance
   .GetReference("users").Child(userID)
   .GetValueAsync().ContinueWith(task =>
   {
       if (task.IsFaulted)
       {
           Debug.LogError("Error retriving user data: " + userID);
         // Handle the error...
       }
       else if (task.IsCompleted)
       {
           DataSnapshot snapshot = task.Result;
           int TempUserLevel = (int)snapshot.Child("Level").Value; 
          //this get's an error 
           PlayerPrefs.SetInt(_UserLevel, TempUserLevel);

       }
   }

Error:错误:

TrySetInt can only be called from the main thread. TrySetInt 只能从主线程调用。 Constructors and field initializers will be executed from the loading thread when loading a scene.加载场景时,将从加载线程执行构造函数和字段初始化程序。

As I understand the TASK is a new thread and not Unity Main thread.据我了解,TASK 是一个新线程而不是 Unity 主线程。 Still I can't seem save values locally on unity, or get the value out of the TASK.我仍然无法在本地统一保存值,或者从任务中获取值。

It cannot be called because continue with is a delegate and it waits for response.它不能被调用,因为 continue with 是一个委托并且它等待响应。 What I did is just made a waituntil coroutine before calling this delegate using and set a bool for instance some bool check = false.我所做的只是在调用此委托之前创建了一个 waituntil 协程,并设置了一个 bool,例如 some bool check = false。

else if(task.IsCompleted)
    {
     // your operation
     check=true;

    }
    ////////
IEnumerator myRoutine()
  {
         yield return new WaitUntil ( () => check );
         // set your playerprefs.

  }

实际上,您可以将“ContinueWith”更改为“ContinueWithOnMainThread”。

You can change ContinueWith to ContinueWithOnMainThread, but you will need add "using Firebase.Extensions;"您可以将 ContinueWith 更改为 ContinueWithOnMainThread,但您需要添加“using Firebase.Extensions;”

using Firebase.Extensions;

FirebaseDatabase.DefaultInstance.GetReference("users").Child(userID)
  .GetValueAsync().ContinueWithOnMainThread(task => 
{
    if (task.IsFaulted) 
    {
      // Handle the error...
    }
    else if (task.IsCompleted) 
    {
      DataSnapshot snapshot = task.Result;
      // Do something with snapshot...
    }
});

See documentation for more examples: https://firebase.google.com/docs/database/unity/retrieve-data有关更多示例,请参阅文档: https://firebase.google.com/docs/database/unity/retrieve-data

Potential Note: If you are following documentation for Google Sign in with Firebase and downloaded the Firebase SDK for Unity 2020, you may get an error regarding a conflict with System.Threading.Tasks (this happened to me).潜在注意事项:如果您正在关注 Google Sign in with Firebase 的文档并下载 Firebase SDK for Unity 2020,您可能会收到有关与 System.Threading.Tasks 冲突的错误(这发生在我身上)。 If anyone else gets this error, it can be dealt with by deleting or renaming the Unity.Compat and Unity.Tasks files under Unity > Assets > Parse > Plugins, but do not change or delete the files in the dotNet45 folder.如果其他人遇到此错误,可以通过删除或重命名 Unity > Assets > Parse > Plugins 下的 Unity.Compat 和 Unity.Tasks 文件来处理,但不要更改或删除 dotNet45 文件夹中的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM