简体   繁体   English

创建领域对象的问题(IllegalStateException:无法在写事务之外修改托管对象)

[英]Problem with creating realm object ( IllegalStateException: Cannot modify managed objects outside of a write transaction)

I got the problem with adding new objects to Realm data base. 我在向Realm数据库添加新对象时遇到问题。 I have Product class extended Realm Object and my main code in Main Activity. 我在主活动中有Product类扩展了Realm对象,并且拥有了我的主要代码。 When I launch the app without 当我没有启动应用程序时

Product product1 = myRealm.createObject(Product.class);

the entered objects are adds (appears on the screen) to Realm List and they disappears when I go to another activity (also a problem but not the case). 输入的对象将添加(显示在屏幕上)到“领域列表”中,并且当我进行其他活动时它们也会消失(也是一个问题,但并非如此)。 I see the reference on that string of code in Logcat. 我在Logcat中看到了该代码字符串的引用。 " java.lang.IllegalStateException: Cannot modify managed objects outside of a write transaction. " java.lang.IllegalStateException:无法在写事务之外修改托管对象。

product is also a string, so now you understand what means 产品也是一个字符串,所以现在您了解了什么意思

setProduct . setProduct

I also have problems when I add 添加时我也遇到问题

myRealm.commitTransaction()

or something. 或者其他的东西。

  public class MainActivity extends AppCompatActivity {
static View view1;
EditText editText;
static RealmList<Product> productRealmList;
static Realm myRealm = Realm.getDefaultInstance();

@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Product TV = new Product("TV");
    Product watch = new Product("Watch");

    productRealmList = new RealmList<>();
    productRealmList.add(TV);
    productRealmList.add(watch);

    MyAdapter adapter = new MyAdapter(this, R.layout.adapter_layout, productRealmList);
    listView.setAdapter(adapter);


    findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.alert_layout, null);
            editText = view1.findViewById(R.id.ent);
            new AlertDialog.Builder(MainActivity.this)
                    .setTitle("Create new product")
                    .setMessage("Put down the name of the new product")
                    .setView(view1)
                    .setNegativeButton("Cancel", null)
                    .setPositiveButton("Add",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
  Product product1 = myRealm.createObject(Product.class);
                                        product1.setProduct(editText.getText().toString());
productRealmList.add(product1);

                                }
                            })

                    .create()
                    .show();
        }
    });

I want to save the products to data base and RealmList when user enters the name of his/her product(s) and presses Add in Alert Dialog. 当用户输入产品名称并在“警报”对话框中按“添加”时,我想将产品保存到数据库和RealmList中。 (want them to be displayed on the screen) (希望它们显示在屏幕上)

You need to create your RealmObject inside a transaction as the exception also tells you. 您需要在事务内创建RealmObject因为异常也会告诉您。

Have a look at: https://realm.io/docs/java/latest/#transaction-blocks 看看: https//realm.io/docs/java/latest/#transaction-blocks

Inside your OnClickListener do this: 在您的OnClickListener内部执行以下操作:

realm.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {
        final Product product1 = myRealm.createObject(Product.class);
        product1.setProduct(editText.getText().toString());
        productRealmList.add(product1);
    }
});

You might have to create your productRealmList inside a transaction as well or at least fetch it from Realm inside your transaction block, but it's a bit difficult for me to test without creating a whole new project with Realm in. 您可能还必须在事务内创建productRealmList ,或者至少从事务块内的Realm中获取它,但是如果不使用Realm创建整个新项目,我很难进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM