C# NET可以';t从另一个线程访问realmobject

C# NET可以';t从另一个线程访问realmobject,c#,realm,C#,Realm,根据答案,我发现我不能从另一个线程访问领域对象,但从答案中,我明白我可以存储realmobject的引用,然后在另一个线程中使用它 我想循环它直到它被插入,但问题是我无法在另一个线程上访问var文档和var s。 那么我怎样才能找到解决这个问题的方法呢 private async void UpdateNotifications_OnAdd(object sender, EventArgs e) { //UpdateNotification is my RealmObject v

根据答案,我发现我不能从另一个线程访问领域对象,但从答案中,我明白我可以存储realmobject的引用,然后在另一个线程中使用它

我想循环它直到它被插入,但问题是我无法在另一个线程上访问
var文档
var s
。 那么我怎样才能找到解决这个问题的方法呢

private async void UpdateNotifications_OnAdd(object sender, EventArgs e)
{
    //UpdateNotification is my RealmObject
    var s = (UpdateNotifications)sender;
    //Here I want to find the document that has the id from my updateNotification
    var document = realm.Find<Document>(s.Identifier)

    await Task.Run(async () =>
    {
        while(!s.Inserted)
        {
            //Here I want to access my document and my S
            string text = queryFinder(document)
            realm.Write(() =>
            {
                s.Inserted = true;
            });
        }
    }
}
private async void UpdateNotifications\u OnAdd(对象发送方,事件参数e)
{
//UpdateNotification是我真正的主题
var s=(更新通知)发送方;
//在这里,我想找到具有我的updateNotification id的文档
var document=realm.Find(s.Identifier)
等待任务。运行(异步()=>
{
而(!s.Inserted)
{
//我想在这里访问我的文档和
string text=queryFinder(文档)
realm.Write(()=>
{
s、 插入=真;
});
}
}
}

答案是,您可以存储对对象Id的引用,然后重新查询(使用
realm.Find
)在后台线程上。虽然有点旧,但现在有一种更好的方法可以做到这一点——使用。也就是说,您的代码将无法工作,因为您在后台线程上使用的是主线程的领域实例,这也是不允许的。您需要对其进行一点重构,使其看起来有点像这样:

private async void UpdateNotifications_OnAdd(object sender, EventArgs e)
{
    var notifications = (UpdateNotifications)sender;
    var document = realm.Find<Document>(notifications.Identifier);

    // Create thread safe references to notifications and document.
    // We'll use them to look up the objects in the background.
    var notificationsRef = ThreadSafeReference.Create(notifications);
    var documentRef = ThreadSafeReference.Create(document);

    await Task.Run(async () =>
    {
        // Always dispose of the Realm on a background thread
        using bgRealm = Realm.GetInstance();

        // We need to look up the notifications and document objects
        // on the background thread from the references we created
        var bgNotifications = bgRealm.Resolve(notificationsRef);
        var bgDocument = bgRealm.Resolve(documentRef;)

        string text = queryFinder(bgDocument);
        bgRealm.Write(() =>
        {
            bgNotifications.Inserted = true;
        });
    }
}
private async void UpdateNotifications\u OnAdd(对象发送方,事件参数e)
{
var通知=(更新通知)发送方;
var document=realm.Find(notifications.Identifier);
//创建对通知和文档的线程安全引用。
//我们将使用它们在背景中查找对象。
var notificationsRef=ThreadSafeReference.Create(通知);
var documentRef=ThreadSafeReference.Create(文档);
等待任务。运行(异步()=>
{
//始终在后台线程上处置域
使用bgream=Realm.GetInstance();
//我们需要查找通知和文档对象
//在我们创建的引用的背景线程上
var bgNotifications=bgream.Resolve(notificationsRef);
var bgDocument=bgream.Resolve(documentRef;)
string text=queryFinder(bgDocument);
bgRealm.Write(()=>
{
bgNotifications.Inserted=true;
});
}
}

答案是,您可以存储对对象Id的引用,然后重新查询(使用
realm.Find
)在后台线程上。虽然有点旧,但现在有一种更好的方法可以做到这一点——使用。也就是说,您的代码将无法工作,因为您在后台线程上使用的是主线程的领域实例,这也是不允许的。您需要对其进行一点重构,使其看起来有点像这样:

private async void UpdateNotifications_OnAdd(object sender, EventArgs e)
{
    var notifications = (UpdateNotifications)sender;
    var document = realm.Find<Document>(notifications.Identifier);

    // Create thread safe references to notifications and document.
    // We'll use them to look up the objects in the background.
    var notificationsRef = ThreadSafeReference.Create(notifications);
    var documentRef = ThreadSafeReference.Create(document);

    await Task.Run(async () =>
    {
        // Always dispose of the Realm on a background thread
        using bgRealm = Realm.GetInstance();

        // We need to look up the notifications and document objects
        // on the background thread from the references we created
        var bgNotifications = bgRealm.Resolve(notificationsRef);
        var bgDocument = bgRealm.Resolve(documentRef;)

        string text = queryFinder(bgDocument);
        bgRealm.Write(() =>
        {
            bgNotifications.Inserted = true;
        });
    }
}
private async void UpdateNotifications\u OnAdd(对象发送方,事件参数e)
{
var通知=(更新通知)发送方;
var document=realm.Find(notifications.Identifier);
//创建对通知和文档的线程安全引用。
//我们将使用它们在背景中查找对象。
var notificationsRef=ThreadSafeReference.Create(通知);
var documentRef=ThreadSafeReference.Create(文档);
等待任务。运行(异步()=>
{
//始终在后台线程上处置域
使用bgream=Realm.GetInstance();
//我们需要查找通知和文档对象
//在我们创建的引用的背景线程上
var bgNotifications=bgream.Resolve(notificationsRef);
var bgDocument=bgream.Resolve(documentRef;)
string text=queryFinder(bgDocument);
bgRealm.Write(()=>
{
bgNotifications.Inserted=true;
});
}
}