dimcot8
dimcot8

Reputation: 53

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. I have Product class extended Realm Object and my main code in Main Activity. 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. "java.lang.IllegalStateException: Cannot modify managed objects outside of a write transaction. "

product is also a string, so now you understand what means

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. (want them to be displayed on the screen)

Upvotes: 0

Views: 1796

Answers (1)

Darwind
Darwind

Reputation: 7371

You need to create your RealmObject inside a transaction as the exception also tells you.

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

Inside your OnClickListener do this:

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.

Upvotes: 2

Related Questions