android - How to save only RealmObject but not referenced object -
in app has following realmobjects:
product - act master data , should never modified.
cart - shopping cart allow people pick stuff buy. content selection type.
selection - represent product user selected alongside preferences such color, size, etc.
use case
user select product , add cart. product wrapped inside selection , stored inside cart. pick product a, b, , c.
now save realm. document tells me use realmlist
add relationship. makes cart -> list; selection -> product.
then if use copytorealm
, i'll primarykey exception on product. since want save only cart , selection, how make selection link product(for reading up) not saving it.
if use copytorealmorupdate
, risk accidentally updating product?
you can create realmobject explicitly inside realm start, , set object link managed object inside managed object.
realm.executetransaction((realm) -> { // assuming primary key selection selection = realm.where(selection.class).equalto(selectionfields.id, selectionid).findfirst(); if(selection == null) { selection = realm.createobject(selection.class, selectionid); } // here, selection managed //... product product = realm.where(product.class)./*...*/.findfirst(); selection.setproduct(product); // no insertorupdate()/copytorealmorupdate() call `selection` });
but can set product link after turned proxy managed.
realm.executetransaction((realm) -> { selection selection = new selection(); // assuming selection.getproduct() == null selection = realm.copytorealmorupdate(selection); // selection managed product product = realm.where(product.class)./*...*/.findfirst(); selection.setproduct(product); // no insertorupdate()/copytorealmorupdate() call `selection` });
Comments
Post a Comment