首页 > 解决方案 > 如何检查 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. 有没有办法检查客户端是否离线,如果是,则将其更改为在线?或者有没有办法告诉客户端不要使用缓存?

标签: c#firebaseunity3dgoogle-cloud-firestoreoffline

解决方案


有没有办法检查客户端是否离线并将其更改为在线?

不,在线状态由 SDK 管理,不通过其 API 公开。

或者有没有办法告诉客户端不要使用缓存?

不幸的是没有。对于其他平台,您可以使用源选项来强制缓存或服务器,但文档表明 Unity 不支持此功能。


推荐阅读