【发布时间】: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