[英]Is there a way to query a main-thread realm instance without blocking the main thread?
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. 我想将一些主线程领域对象传递给某些视图模型,但不想在检索它们时阻塞UI。
I need realm objects from a main-thread realm instance so that myRealmObject.PropertyChanged is called on the main thread. 我需要来自主线程领域实例的领域对象,以便在主线程上调用myRealmObject.PropertyChanged。 If no background query, is there a way to make a background-thread realm object's PropertyChanged be called on the main thread? 如果没有后台查询,是否有办法在主线程上调用后台线程领域对象的PropertyChanged?
You can query on a background thread and create a ThreadSafeReference
that you can pass to your VMs. 您可以在后台线程上查询并创建可以传递给VM的ThreadSafeReference
。 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 然后在您的ViewModel中,您可以拥有
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. 查看文档以获取更多详细说明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.