fix cloud gacha download crash

This commit is contained in:
Lightczx
2023-04-07 16:41:57 +08:00
parent 32177491da
commit d5b668cb8d
3 changed files with 17 additions and 2 deletions

View File

@@ -10,6 +10,20 @@ namespace Snap.Hutao.Extension;
/// </summary> /// </summary>
internal static partial class EnumerableExtension internal static partial class EnumerableExtension
{ {
/// <summary>
/// 尝试添加物品
/// </summary>
/// <typeparam name="T">物品类型</typeparam>
/// <param name="collection">集合</param>
/// <param name="item">物品</param>
public static void AddIfNotContains<T>(this Collection<T> collection, T item)
{
if (!collection.Contains(item))
{
collection.Add(item);
}
}
/// <summary> /// <summary>
/// 移除集合中满足条件的项 /// 移除集合中满足条件的项
/// </summary> /// </summary>

View File

@@ -311,7 +311,7 @@ internal sealed class GachaLogService : IGachaLogService
{ {
if (archive == null) if (archive == null)
{ {
archive = appDbContext.GachaArchives.SingleOrDefault(a => a.Uid == uid); archive = appDbContext.GachaArchives.AsNoTracking().SingleOrDefault(a => a.Uid == uid);
if (archive == null) if (archive == null)
{ {
@@ -320,7 +320,7 @@ internal sealed class GachaLogService : IGachaLogService
// System.InvalidOperationException: Sequence contains no elements // System.InvalidOperationException: Sequence contains no elements
// ? how this happen here? // ? how this happen here?
archive = appDbContext.GachaArchives.Single(a => a.Uid == uid); archive = appDbContext.GachaArchives.AsNoTracking().Single(a => a.Uid == uid);
GachaArchive temp = archive; GachaArchive temp = archive;
ThreadHelper.InvokeOnMainThread(() => archiveCollection!.Add(temp)); ThreadHelper.InvokeOnMainThread(() => archiveCollection!.Add(temp));
} }

View File

@@ -321,6 +321,7 @@ internal sealed class GachaLogViewModel : Abstraction.ViewModel
if (isOk) if (isOk)
{ {
await ThreadHelper.SwitchToMainThreadAsync(); await ThreadHelper.SwitchToMainThreadAsync();
Archives?.AddIfNotContains(archive!);
SetSelectedArchiveAndUpdateStatistics(archive, true); SetSelectedArchiveAndUpdateStatistics(archive, true);
} }
} }