Xamarin 表单 - 从错误线程访问的领域 [英] Xamarin forms - Realm accessed from incorrect thread

查看:70
本文介绍了Xamarin 表单 - 从错误线程访问的领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我在这里遗漏了一些非常简单的东西,但无论如何都会问......

Maybe I'm missing something really simple out here but gonna ask anyways.....

我正在使用 Xamarin 表单(.NET Standard 项目)、MVVMLight、Realm DB 和 ZXing Barcode Scanner.

I am using Xamarin forms (.NET Standard project), MVVMLight, Realm DB and ZXing Barcode Scanner.

我有一个像这样的领域对象...

I have a realmobject like so...

public class Participant : RealmObject
{
    public string FirstName {get; set;}
    public string LastName {get; set;}
    public string Email {get; set;}
    public string RegistrationCode {get; set;}

    //More properties skipped out for brevity
}

我有对应的viewmodel如下:

I have the corresponding viewmodel as follows:

public class ParticipantViewModel
{
    Realm RealmInstance
    public ParticipantViewModel()
    {
        RealmInstance = Realms.Realm.GetInstance();
        RefreshParticipants();
    }

    private async Task RefreshParticipants() 
    {
        //I have code here that GETS the list of Participants from an API and saves to the device.
        //I am using the above-defined RealmInstance to save to IQueryable<Participant> Participants
    }
}

以上所有工作正常,我对此没有任何问题.在同一个视图模型中,我还可以启动 ZXing 扫描器并扫描代表注册码的条形码.

All the above works fine and I have no issues with this. In the same viewmodel, I am also able to fire up the ZXing Scanner and scan a bar code representing a RegistrationCode.

这反过来会在扫描后填充以下属性(也在视图模型中)...

This, in turn, populates the below property (also in the viewmodel) once scanned...

    private ZXing.Result result;
    public ZXing.Result Result
    {
        get { return result; }
        set { Set(() => Result, ref result, value); }
    }

并调用以下方法(通过 ScanResultCommand 连接)以获取携带扫描注册码的参与者.

and calls the below method (wired up via the ScanResultCommand) to fetch the participant bearing the scanned RegistrationCode.

    private async Task ScanResults()
    {
        if (Result != null && !String.IsNullOrWhiteSpace(Result.Text))
        {
            string regCode = Result.Text;
            await CloseScanner();
            SelectedParticipant = Participants.FirstOrDefault(p => p.RegistrationCode.Equals(regCode, StringComparison.OrdinalIgnoreCase));
            if (SelectedParticipant != null)
            {
                //Show details for the scanned Participant with regCode
            }
            else
            {
                //Display not found message
            }
        }
    }

我不断收到以下错误....

I keep getting the below error....

System.Exception: Realm 从错误的线程访问.

由下面的行生成....

generated by the line below....

SelectedParticipant = Participants.FirstOrDefault(p => p.RegistrationCode.Equals(regCode, StringComparison.OrdinalIgnoreCase));

我不确定这是一个不正确的线程,但任何关于如何从已经填充的 IQueryable 或直接从 Realm 表示中获取扫描的参与者的想法都将不胜感激.

I'm not sure how this is an incorrect thread but any ideas on how I can get around to fetching the scanned participant either from the already populated IQueryable or from the Realm representation directly would be greatly appreciated.

谢谢

推荐答案

是的,您正在构造函数中获得一个领域实例,然后从异步任务(或线程)中使用它.您只能从获得引用的线程访问领域.由于您仅使用默认实例,因此您应该能够在使用它的函数(或线程)中简单地获取本地引用.尝试使用

Yes, you're getting a realm instance in the constructor, and then using it from an async task (or thread). You can only access a realm from the thread in which you obtained the reference. Since you're only using a default instance, you should be able to simply obtain a local reference within the function (or thread) where you use it. Try using

    Realm LocalInstance = Realms.Realm.GetInstance();

在函数的顶部并使用它.您需要重新创建 Participants 查询以使用与其源相同的实例.无论您在何处使用异步任务(线程),都会出现这种情况,因此要么更改 all 以获取进入时的默认实例,要么减少访问该领域的线程数.

at the top of the function and use that. You'll need to recreate the Participants query to use the same instance as its source too. This will be the case wherever you use async tasks (threads), so either change all to get hold of the default instance on entry or reduce the number of threads that access the realm.

顺便说一句,我很惊讶您没有从 中收到类似的访问错误RefreshParticipants() - 也许您实际上并没有从那里通过 RealmInstance 访问数据.

Incidentally I'm surprised you don't get a similar access error from within RefreshParticipants() - maybe you're not actually accessing data via RealmInstance from there.

这篇关于Xamarin 表单 - 从错误线程访问的领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆