【问题标题】:How do I check if I'm connected to Firebase?如何检查我是否已连接到 Firebase?
【发布时间】:2020-08-12 11:56:18
【问题描述】:

我有一个适用于 Android 的 Firebase Unity 项目。我目前正在使用 Unity 2019.3.1 并使用 Firebase SDK 6.15.2。我有一个检查文档是否存在的函数:

public async void GetSpecificUserDocumentAsync(DocumentReference docRef, string onComplete)
{
    Debug.Log(String.Format("Getting document {0} from database!", onComplete));
    DocumentSnapshot task = await docRef.GetSnapshotAsync();
    if (task.Exists)
    {
        Debug.Log(String.Format("Document data for {0} document:", task.Id));
    }
    else
    {
        Debug.Log(String.Format("Document does not exist!"));
    }
}

但是,即使我的计算机在线,控制台也会不断返回错误 FirebaseException: Failed to get document because the client is offline.

我已经上网查找可能的修复或检查并找到了检查我是否在线的方法:

async void Func()
{
    List<Task> tasks = new List<Task>
    {
        GetDatabaseNode("path_to_node", SampleCallback)
    };
    //Add more tasks here...

    int timeout = 10000;
    Task timeoutTask = Task.Delay(timeout);

    if (await Task.WhenAny(Task.WhenAll(tasks), timeoutTask) == timeoutTask)
    {
        // timeout logic
        Debug.Log("TIMED OUT");

    }
    else
    {
        // task completed within timeout 
    }
}
public async Task GetDatabaseNode(string path, System.Action<DocumentSnapshot> callback)
{
    await database.Collection("Data").Document("yytt").GetSnapshotAsync().ContinueWith(task =>
    {
        if (task.IsFaulted || task.IsCanceled)
        {
            Debug.Log("Inner task was " + (task.IsFaulted ? "faulted." : "cancelled."));
            return;
        }

        callback?.Invoke(task.Result);
    });

    // anything that you put here will be run once the awaiting above has finished
}

private void SampleCallback(DocumentSnapshot snapshot)
{
    foreach (KeyValuePair<string, object> childSnapshot in snapshot.ToDictionary())
    {
        Debug.LogFormat("node contains: {0}", childSnapshot.Key);
    }
}

但是,任务总是会出错,并且会立即进入 TIMEOUT。发生这种情况时有没有办法连接到网络?

【问题讨论】:

    标签: firebase unity3d google-cloud-firestore


    【解决方案1】:

    如何检查我是否已连接到 Firebase?

    Firestore SDK 不提供任何方法来确定它是否认为自己已连接。您必须相信它最终会重试任何断开的连接。

    如果您认为 SDK 有误,并且您有重现问题的具体步骤,请向Firebase support 提交错误报告。

    【讨论】:

      猜你喜欢
      • 2012-08-05
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 1970-01-01
      • 1970-01-01
      • 2019-08-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多