Fix `RealmLive` failing to retrieve due to lack of refresh

This commit is contained in:
Dean Herbert 2022-01-21 17:33:03 +09:00
parent d2655c0825
commit 81b5717ae7
1 changed files with 18 additions and 13 deletions

View File

@ -53,18 +53,7 @@ public void PerformRead(Action<T> perform)
realmFactory.Run(realm =>
{
var found = realm.Find<T>(ID);
if (found == null)
{
// It may be that we access this from the update thread before a refresh has taken place.
// To ensure that behaviour matches what we'd expect (the object *is* available), force
// a refresh to bring in any off-thread changes immediately.
realm.Refresh();
found = realm.Find<T>(ID);
}
perform(found);
perform(retrieveFromID(realm, ID));
});
}
@ -79,7 +68,7 @@ public TReturn PerformRead<TReturn>(Func<T, TReturn> perform)
return realmFactory.Run(realm =>
{
var returnData = perform(realm.Find<T>(ID));
var returnData = perform(retrieveFromID(realm, ID));
if (returnData is RealmObjectBase realmObject && realmObject.IsManaged)
throw new InvalidOperationException(@$"Managed realm objects should not exit the scope of {nameof(PerformRead)}.");
@ -119,6 +108,22 @@ public T Value
}
}
private T retrieveFromID(Realm realm, Guid id)
{
var found = realm.Find<T>(ID);
if (found == null)
{
// It may be that we access this from the update thread before a refresh has taken place.
// To ensure that behaviour matches what we'd expect (the object *is* available), force
// a refresh to bring in any off-thread changes immediately.
realm.Refresh();
found = realm.Find<T>(ID);
}
return found;
}
public bool Equals(ILive<T>? other) => ID == other?.ID;
public override string ToString() => PerformRead(i => i.ToString());