作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题听起来与之前提出的问题相似。我已经引用了这些,但找不到解决我遇到的这个问题的方法。
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());
从我的理解来看,这段代码似乎是可行的,但它并不适用,因为我在同一个线程中异步访问对象。我在这里错过了什么?
最佳答案
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);
第二种解决方案更干净。
关于android - realmio - Realm 对象只能从它们创建的线程访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39200670/
有一条(相对)众所周知的 Perl 公理:“只有 Perl 可以解析 Perl”。我想知道 Perl 6 是否仍然如此? 扩大讨论...考虑到 PyPy 最近的更新,我想到了这个问题。 Perl 独特
这是设置。在上一个问题中,我发现我可以通过子组件中的状态传递对象属性,然后使用 componentDidUpdate 获取该对象属性。在这种情况下,状态和属性都称为到达。 这是基本代码... expo
我运行的是 10.5.2 社区版。我已经标记了 源/主要/资源 作为源目录。我可以右键单击并“编译”某些文件,据我所知,这意味着 IDE 将文件复制到与发送类文件的“com.mydomain.pack
我是一名优秀的程序员,十分优秀!