c# - 如何检查 Firestore 客户端是否离线并在 Unity 上更改为在线?
问题描述
我正在运行 Unity 2019.3.1。我一直在尝试使用以下代码从 Firestore 获取文档:
// docRef = database.Collection("Data").Document(documentName)
// database data type = FirebaseFirestore
public IEnumerator GetSpecificUserDocumentAsync(DocumentReference docRef, string variable)
{
Debug.Log(String.Format("Getting document {0} from database!", variable));
yield return database.EnableNetworkAsync();
Task<DocumentSnapshot> task = docRef.GetSnapshotAsync();
yield return new WaitForTaskCompletion(task);
if (task.Result.Exists)
{
data[variable] = true;
OnFireStoreResult.Invoke();
Debug.Log(String.Format("Document data for {0} document:", task.Result.Id));
}
else
{
data[variable] = false;
OnFireStoreResult.Invoke();
Debug.Log(String.Format("Document does not exist!"));
}
}
// WaitForTaskCompletion is within the same class as the above function
class WaitForTaskCompletion : CustomYieldInstruction
{
Task task;
// Create an enumerator that waits for the specified task to complete.
public WaitForTaskCompletion(Task task)
{
instance.previousTask = task;
instance.operationInProgress = true;
this.task = task;
}
// Wait for the task to complete.
public override bool keepWaiting
{
get
{
if (task.IsCompleted)
{
instance.operationInProgress = false;
instance.cancellationTokenSource = new CancellationTokenSource();
if (task.IsFaulted) Debug.Log(task.Exception.ToString());
return false;
}
return true;
}
}
}
但是,当我尝试检索文档时,Unity 总是会显示此错误:System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or more errors occurred. ---> Firebase.FirebaseException: Failed to get document because the client is offline.
有没有办法检查客户端是否离线,如果是,则将其更改为在线?或者有没有办法告诉客户端不要使用缓存?
解决方案
有没有办法检查客户端是否离线并将其更改为在线?
不,在线状态由 SDK 管理,不通过其 API 公开。
或者有没有办法告诉客户端不要使用缓存?
不幸的是没有。对于其他平台,您可以使用源选项来强制缓存或服务器,但文档表明 Unity 不支持此功能。
推荐阅读
- batch-file - CMD 如何为每个搜索结果设置不同的变量
- java - 上传文件后如何获取下载地址
- c# - 如果 DbContext 的 OnConfiguring 方法中的所有配置,如何使用 AddDbContextPool
- android - 更新 Android Studio 后是否在现有项目上启用了 r8?
- jsf - 为什么 GlassFish 5.1.0 中的 JSF 将我的 @ViewScoped CDI bean 的 ID 属性设置为 null?
- c++ - 我应该使用什么类型的稀疏向量?
- sapui5 - 当您按下表格上的删除按钮时,如何获取行值?
- r - 在 R 中使用 lapply 时缺少参数
- unity3d - Unity 按钮 onclick 不触发功能(通过 Unity 界面链接)
- jenkins - 如何使用 Nuget.exe 获取特定安装的 Nuget 包的版本?