无法访问已处置对象 - Xamarin.Forms / FirebaseFirestore,这是错误还是我的错?

Cannot Access Disposed Object - Xamarin.Forms / FirebaseFirestore, is it bug or my fault?

您好,我在使用 Xamarin.Forms 和 FirebaseFirestore 时遇到问题 该代码有时会抛出“无法访问已处理对象”(特别是在第二次调用时) 该代码在 Android 项目中使用。

如果有人遇到过这种情况,我希望得到您的任何建议。 提前致谢。

 public async Task<ObservableCollection<PortfolioDetails>> GetPortfoliosAsync()
       {

           ObservableCollection<PortfolioDetails> PortfolioList = new ObservableCollection<PortfolioDetails>();

           object _result = await FirebaseFirestore.Instance
               .Collection(Firebase.Auth.FirebaseAuth.Instance.CurrentUser.Uid)
               .Get();

           if (_result is QuerySnapshot docs)
           {

               foreach (DocumentSnapshot y in docs.Documents)
               {

                   var _tcs = new TaskCompletionSource<PortfolioDetails>();


                   var secondResult = await FirebaseFirestore.Instance
                     .Collection(Firebase.Auth.FirebaseAuth.Instance.CurrentUser.Uid)
                     .Document(y.Id)
                     .Collection("Transactions")
                     .Get();
                   var result = new PortfolioDetails();

                   if (secondResult is QuerySnapshot _docs)
                   {
                       CultureInfo.CurrentCulture = new CultureInfo("en-US", false);
                       decimal portfolioValue = 0;
                       decimal portfolioBuyPrice = 0;
                       foreach (var i in _docs.Documents)
                       {
                           portfolioBuyPrice += decimal.Parse(i.GetString("qty")) * decimal.Parse(i.GetString("buyprice"));

                           switch (i.GetString("type"))
                           {
                               case "stock":
                                   var securities = await Yahoo.Symbols(i.Id).Fields(Field.RegularMarketPrice).QueryAsync();
                                   var symbol = securities[i.Id];
                                   portfolioValue += decimal.Parse(symbol[Field.RegularMarketPrice].ToString()) * decimal.Parse(i.GetString("qty"));
                                   break;

                               case "crypto":
                                   var price = AllClass.Data.Find(x => x.id.Contains(i.Id));
                                   portfolioValue += decimal.Parse(price.current_price) * decimal.Parse(i.GetString("qty"));
                                   break;
                           }
                       }


                       result.Price = "$" + portfolioValue;

                       if (portfolioValue > 10000 || portfolioValue < -10000)
                       {
                           result.Price = "$" + Math.Round(portfolioValue / 1000M, 2) + "K";
                       }
                       if (portfolioValue > 1000000 || portfolioValue < -1000000)
                       {
                           result.Price = "$" + Math.Round(portfolioValue / 1000000M, 2) + "M";
                       }

                       var valueDifference = portfolioValue - portfolioBuyPrice;
                       var tempValueDifference = valueDifference.ToString();

                       string valueDifferenceString;
                       if (valueDifference > 0)
                       {
                           result.BuyPrice_Color = "#4bb543";
                           if (valueDifference > 10000)
                           {
                               tempValueDifference = Math.Round(valueDifference / 1000M, 2) + "K";
                           }
                           if (portfolioValue > 1000000)
                           {
                               tempValueDifference = Math.Round(valueDifference / 1000000M, 2) + "M";
                           }
                           valueDifferenceString = "+$" + tempValueDifference;
                       }
                       else
                       {
                           result.BuyPrice_Color = "#ff0033";
                           if (valueDifference < -10000)
                           {
                               tempValueDifference = Math.Round(valueDifference / 1000M, 2) + "K";
                           }
                           if (valueDifference < -1000000)
                           {
                               tempValueDifference = Math.Round(valueDifference / 1000000M, 2) + "M";
                           }
                           valueDifferenceString = "-$" + tempValueDifference.Replace("-", "");
                       }

                       var percentageDifference = Math.Round((portfolioValue - portfolioBuyPrice) / (portfolioBuyPrice / 100), 2);
                       string percentageDifferenceString;
                       if (percentageDifference > 0)
                       {
                           percentageDifferenceString = "(+" + percentageDifference + "%)";
                       }
                       else
                       {
                           percentageDifferenceString = "(" + percentageDifference + "%)";
                       }
                       result.BuyPrice = valueDifferenceString + " " + percentageDifferenceString;
                   }
                       PortfolioDetails PortfolioObj = result;
                   PortfolioObj.PortfolioName = y.Id;
                   PortfolioList.Add(PortfolioObj);
               }
           }
               return PortfolioList;
       }

Firebase 的新实例有时会触发 GC,因此在新实例之前我将所有数据处理为 C# 类型,这样,Java GC 不会触及该数据。