【发布时间】:2020-09-29 22:12:32
【问题描述】:
Task.Factory.StartNew(async () =>
{
try
{
ShowCaseInfo existingShowcase = DBService.GetDB().FetchShowcaseInfo();
string previousResponse = existingShowcase?.SerializedResponse;
Response response = await CloudService.GetCloudService().FetchShowcase();
if (response.Status == ResponseStatus.SUCCESS && !string.Equals(previousResponse, response.Data))
{
ShowCaseInfo showcaseInfo = JsonConvert.DeserializeObject<ShowCaseInfo>(response.Data, _settings);
showcaseInfo.SerializedResponse = response.Data;
DBService.GetDB().InsertOrUpdateShowcase(showcaseInfo);
FetchShowcaseProducts(showcaseInfo.Showcases);
}
else
{
List<Showcase> emptyCases = new List<Showcase>();
if (existingShowcase != null)
{
foreach (Showcase showcase in existingShowcase.Showcases)
{
if (showcase.Products.Count == 0)
{
emptyCases.Add(showcase);
}
}
}
FetchShowcaseProducts(emptyCases);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
});
foreach(现有Showcase.Showcases 中的Showcase 展示)行正在引发异常。同样,在 if 条件 !string.Equals(previousResponse, response.Data) 中,如果我将 previousResponse 作为 existingShowcase.SerializedResponse 访问,而不是局部变量,则会引发一些异常。根据文档,我们不应该跨线程传递对象,但在这种情况下,所有操作都在同一个线程中。
【问题讨论】:
标签: c# .net xamarin.forms realm realm-mobile-platform