Reputation: 4955
realm-dotnet
I'd like to pass some main-thread realm objects to some view-models, but don't want to block the UI while I retrieve them.
I need realm objects from a main-thread realm instance so that myRealmObject.PropertyChanged is called on the main thread. If no background query, is there a way to make a background-thread realm object's PropertyChanged be called on the main thread?
Upvotes: 0
Views: 503
Reputation: 2186
You can query on a background thread and create a ThreadSafeReference
that you can pass to your VMs. For example:
var reference = await Task.Run(() =>
{
using (var realm = Realm.GetInstance())
{
var modelToPass = realm.All<MyModel>().Where(...).FirstOrDefault();
return ThreadSafeReference.Create(modelToPass);
}
});
// Pass reference to your ViewModel
Then in your ViewModel you can have
public void Initialize(ThreadSafeReference.Object<MyModel> reference)
{
var realm = Realm.GetInstance();
var myModel = realm.ResolveReference(reference);
// Do stuff with myModel - it's a main thread reference to
// the model you resolved on the background thread
}
Check out the docs for more detailed explanation.
Upvotes: 2