mirror of
https://github.com/ppy/osu.git
synced 2025-01-21 20:33:01 +08:00
Detach beatmap sets asynchronously
This commit is contained in:
parent
b1f653899c
commit
c2c1dccf2d
@ -4,6 +4,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using osu.Framework.Allocation;
|
using osu.Framework.Allocation;
|
||||||
using osu.Framework.Bindables;
|
using osu.Framework.Bindables;
|
||||||
using osu.Framework.Graphics;
|
using osu.Framework.Graphics;
|
||||||
@ -30,9 +31,10 @@ namespace osu.Game.Database
|
|||||||
}
|
}
|
||||||
|
|
||||||
[BackgroundDependencyLoader]
|
[BackgroundDependencyLoader]
|
||||||
private void load()
|
private void load(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
realmSubscription = realm.RegisterForNotifications(getBeatmapSets, beatmapSetsChanged);
|
realmSubscription = realm.RegisterForNotifications(r => r.All<BeatmapSetInfo>().Where(s => !s.DeletePending && !s.Protected), beatmapSetsChanged);
|
||||||
|
loaded.Wait(cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void beatmapSetsChanged(IRealmCollection<BeatmapSetInfo> sender, ChangeSet? changes)
|
private void beatmapSetsChanged(IRealmCollection<BeatmapSetInfo> sender, ChangeSet? changes)
|
||||||
@ -50,23 +52,56 @@ namespace osu.Game.Database
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
detachedBeatmapSets.Clear();
|
// Detaching beatmaps takes some time, so let's make sure it doesn't run on the update thread.
|
||||||
detachedBeatmapSets.AddRange(sender.Detach());
|
var frozenSets = sender.Freeze();
|
||||||
|
|
||||||
|
Task.Factory.StartNew(() =>
|
||||||
|
{
|
||||||
|
realm.Run(_ =>
|
||||||
|
{
|
||||||
|
var detached = frozenSets.Detach();
|
||||||
|
|
||||||
|
detachedBeatmapSets.Clear();
|
||||||
|
detachedBeatmapSets.AddRange(detached);
|
||||||
|
loaded.Set();
|
||||||
|
});
|
||||||
|
}, TaskCreationOptions.LongRunning);
|
||||||
|
|
||||||
loaded.Set();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (int i in changes.DeletedIndices.OrderDescending())
|
foreach (int i in changes.DeletedIndices.OrderDescending())
|
||||||
detachedBeatmapSets.RemoveAt(i);
|
removeAt(i);
|
||||||
|
|
||||||
foreach (int i in changes.InsertedIndices)
|
foreach (int i in changes.InsertedIndices)
|
||||||
{
|
insert(sender[i].Detach(), i);
|
||||||
detachedBeatmapSets.Insert(i, sender[i].Detach());
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (int i in changes.NewModifiedIndices)
|
foreach (int i in changes.NewModifiedIndices)
|
||||||
detachedBeatmapSets.ReplaceRange(i, 1, new[] { sender[i].Detach() });
|
replaceRange(sender[i].Detach(), i);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void replaceRange(BeatmapSetInfo set, int i)
|
||||||
|
{
|
||||||
|
if (loaded.IsSet)
|
||||||
|
detachedBeatmapSets.ReplaceRange(i, 1, new[] { set });
|
||||||
|
else
|
||||||
|
Schedule(() => { detachedBeatmapSets.ReplaceRange(i, 1, new[] { set }); });
|
||||||
|
}
|
||||||
|
|
||||||
|
private void insert(BeatmapSetInfo set, int i)
|
||||||
|
{
|
||||||
|
if (loaded.IsSet)
|
||||||
|
detachedBeatmapSets.Insert(i, set);
|
||||||
|
else
|
||||||
|
Schedule(() => { detachedBeatmapSets.Insert(i, set); });
|
||||||
|
}
|
||||||
|
|
||||||
|
private void removeAt(int i)
|
||||||
|
{
|
||||||
|
if (loaded.IsSet)
|
||||||
|
detachedBeatmapSets.RemoveAt(i);
|
||||||
|
else
|
||||||
|
Schedule(() => { detachedBeatmapSets.RemoveAt(i); });
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void Dispose(bool isDisposing)
|
protected override void Dispose(bool isDisposing)
|
||||||
@ -75,7 +110,5 @@ namespace osu.Game.Database
|
|||||||
loaded.Set();
|
loaded.Set();
|
||||||
realmSubscription?.Dispose();
|
realmSubscription?.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
private IQueryable<BeatmapSetInfo> getBeatmapSets(Realm realm) => realm.All<BeatmapSetInfo>().Where(s => !s.DeletePending && !s.Protected);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user