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

时间:2020-08-12 11:56:18

标签: firebase unity3d google-cloud-firestore

我有一个适用于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);
    }
}

但是,任务将始终出错,并且将立即进入超时。发生这种情况时是否可以连接到网络?

1 个答案:

答案 0 :(得分:0)

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

Firestore SDK无法提供任何方法来确定它是否已连接。您必须相信,它将最终重试所有断开的连接。

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