【问题标题】:realmio - realm objects can only be accessed from thread they were createdrealmio - 只能从创建它们的线程访问领域对象
【发布时间】:2016-08-29 07:27:00
【问题描述】:

问题听起来与之前提出的问题相似。我已经提到了这些,但找不到解决我遇到的这个问题的方法。

private AddCartItemDialog.CartItemListener cartItemListener = new AddCartItemDialog.CartItemListener() {
        @Override
        public void onOkClick(Product cartItem, int quantity) {

            realm.executeTransactionAsync(new Realm.Transaction() {
                @Override
                public void execute(Realm bgRealm) {

                    DraftInvoice draftInvoice = bgRealm.where(DraftInvoice.class).equalTo("shop.id", shopId).findFirst();


                    InvoiceItem invoiceItem = bgRealm.createObject(InvoiceItem.class);
                    invoiceItem.setPrice(cartItem.getPrice());
                    invoiceItem.setId(cartItem.getId());
                    invoiceItem.setQuantity(quantity);
                    invoiceItem.calculateTotal();

                    draftInvoice.getInvoiceItems().add(invoiceItem);

                    updateCartItemCount(draftInvoice.getInvoiceItems().size());
                }
            }, () -> {

            }, error -> {
                error.printStackTrace();
                Logger.e(error.getMessage());
            });

        }

        @Override
        public void onCancelClick() {

        }
    };

错误日志显示以下错误 -

08-28 15:10:25.214 4996-4996/com.example.realshoptest W/System.err: java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.
08-28 15:10:25.214 4996-4996/com.example.realshoptest W/System.err:     at io.realm.BaseRealm.checkIfValid(BaseRealm.java:449)
08-28 15:10:25.214 4996-4996/com.example.realshoptest W/System.err:     at io.realm.ProductRealmProxy.realmGet$price(ProductRealmProxy.java:159)
08-28 15:10:25.214 4996-4996/com.example.realshoptest W/System.err:     at com.example.realshoptest.models.Product.getPrice(Product.java:75)
08-28 15:10:25.214 4996-4996/com.example.realshoptest W/System.err:     at com.example.realshoptest.NewInvoiceActivity$1$1.execute(NewInvoiceActivity.java:76)
08-28 15:10:25.214 4996-4996/com.example.realshoptest W/System.err:     at io.realm.Realm$1.run(Realm.java:1187)
08-28 15:10:25.214 4996-4996/com.example.realshoptest W/System.err:     at io.realm.internal.async.BgPriorityRunnable.run(BgPriorityRunnable.java:34)
08-28 15:10:25.214 4996-4996/com.example.realshoptest W/System.err:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
08-28 15:10:25.214 4996-4996/com.example.realshoptest W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-28 15:10:25.214 4996-4996/com.example.realshoptest W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-28 15:10:25.214 4996-4996/com.example.realshoptest W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-28 15:10:25.214 4996-4996/com.example.realshoptest W/System.err:     at java.lang.Thread.run(Thread.java:818)
08-28 15:10:25.214 4996-4996/com.example.realshoptest E/TestShopApp: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.

此行发生错误 - invoiceItem.setPrice(cartItem.getPrice());

根据我的理解,这段代码似乎可以工作,但因为我在同一个线程中异步访问对象,所以它不起作用。我在这里错过了什么?

【问题讨论】:

  • 您正在访问 cartItem
  • @TimCastelijns 是的,谢谢您的指出。如何将cartItem 传递给对话框,更新其数量并将其返回到侦听器中?
  • 传递 id 并查询执行块内的对象

标签: android thread-safety realm


【解决方案1】:
public void onOkClick(Product cartItem, int quantity) {

此行从 UI 线程的 Realm 实例接收 Product

        realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm bgRealm) {

此调用为后台线程创建一个 Realm 实例

                invoiceItem.setPrice(cartItem.getPrice());

cartItem仍属于UI线程的Realm实例,因此无法在后台线程访问

两种解决方案:

1.) 只将参数发送到后台线程

    public void onOkClick(Product cartItem, int quantity) {
        final long cartItemId = cartItem.getId();
        final String price = cartItem.getPrice();
        realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm bgRealm) {
                DraftInvoice draftInvoice = bgRealm.where(DraftInvoice.class).equalTo("shop.id", shopId).findFirst();
                InvoiceItem invoiceItem = bgRealm.createObject(InvoiceItem.class);
                invoiceItem.setPrice(price);
                invoiceItem.setId(cartItemId);

2.) 使用后台线程的 Realm 实例重新查询对象

    public void onOkClick(Product cartItem, int quantity) {
        final long cartItemId = cartItem.getId();
        realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm bgRealm) {
                Product product = bgRealm.where(Product.class).equalTo("id", cartItemId).findFirst();
                DraftInvoice draftInvoice = bgRealm.where(DraftInvoice.class).equalTo("shop.id", shopId).findFirst();
                InvoiceItem invoiceItem = bgRealm.createObject(InvoiceItem.class);
                invoiceItem.setPrice(product.getPrice());
                invoiceItem.setId(cartItemId);

第二种解决方案更清洁。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多