firebase - Firebase Unity SDK 是否在主线程上引发异常?

标签 firebase unity-game-engine

我的方法之一中的后半部分代码没有运行。 Unity 默认将主线程异常记录到控制台。即使我的函数在主线程上运行,我也没有在控制台中看到任何错误日志。这使得查找错误和修复代码变得非常麻烦。

这是我的简化方法:

    private void DoInitialize() {
        LogWarn($"HERE 1.5 item {i}"); // this prints
        LogWarn($"THIS SHIT {i.shardPrice.amount}"); // this causes a null pointer exception
        LogWarn($"HERE 1.6 item {i}"); // this doesn't print
    }

我连接了调试器并逐步执行代码,直到找到导致问题的行。注释掉该行将使程序正确运行。经过检查,发现该行有一个空指针异常。

我如何知道代码正在主线程上运行?我打印Thread.CurrentThread.ManagedThreadId.ToString() 在错误行之前和 Start() 回调中,并确保 id 相同。

这是线程进入某个我没有符号的随机位置之前的堆栈跟踪。我对此的唯一解释是堆栈底部的 Firebase 方法捕获异常并且不让它传播到 Unity。 enter image description here

这是我的问题:

  1. 我的诊断是否遗漏了某些内容?
  2. 如何确认 Firebase 确实捕获了异常? SDK代码不在github上,所以我无法查看代码。 Firebase 团队的人员可以验证这一点吗?

最佳答案

我意识到,由于 firebase 使用 C# 任务,因此预计会捕获异常。我编写了一个辅助方法来将任务异常打印到控制台。您可以将它用于您期望成功的任务(就像普通的 Unity 代码一样)。

    public static class TaskExts {
        public static void LogExceptionIfFaulted(this Task task) {
            task.ContinueWithOnMainThread(t => {
                if (t.IsFaulted) {
                    Debug.LogException(t.Exception.Flatten().InnerException);
                }
            });
        }
    }

使用

    FirebaseRemoteConfig.FetchAsync(TimeSpan.Zero).ContinueWithOnMainThread(task => {
        // custom code which may throw exceptions
    }).LogExceptionIfFaulted();

关于firebase - Firebase Unity SDK 是否在主线程上引发异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65823485/

相关文章:

ReactJS+FireStore 数据映射问题

firebase - 类型 'UploadMappingFileTask' 属性 'googleServicesResourceRoot' 没有配置值

matrix - (Unity3D)MultiplyPoint 和 MultiplyPoint3x4 之间的区别

Angular 12 : Firebase modulesnot correctly provided (? )

ios - 无法检索数据并在 UIBarButton 中使用它 - Firebase Swift 3

xcode - 使用 Firebase In App Messaging 插件时 Flutter 无法编译项目

c# - 获取 azure 上虚拟文件夹中的 blob (BlobService.ListBlobs) (C#)

unity-game-engine - OnBecameInvisible 未按要求运行

c# - StartCoroutine 函数不适用于 android,但在 Unity 编辑器中运行良好

unity-game-engine - 批处理与 Tris 性能