realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

sharing objects between threads

Open idok595 opened this issue 9 years ago • 2 comments

in realm there is limitiation objects cannot be shared between diffrent thread, in order to do complex search + complex insert/remove/update query async, the only way is to be aware of the object structure and get some how the object id then copy the id to diffrent thread, and select this item on a diffrent thread, i think realm need to have some built in functility to copy objects from one thread to another asyncronycly, here is example of what i am doing now

abstract class ProductTransaction  implements Realm.Transaction {
    public final long productId;

    ProductTransaction(Product product) {
        this.productId = product.getProductId();
    }

    public abstract void execute(Realm realm, Product product);

    @Override
    public void execute(Realm realm) {
        execute(realm, realm.where(Product.class)
                .equalTo("productId", productId)
                .findFirst());
    }
}

i belive realm need to have something like this


 abstract class ObjectTransactions<Model extends RealmObject>  implements Realm.Transaction {

        private Collection<Model> objects;

        ObjectTransactions(Collection<Model> objects) {
            this.objects = objects;
        }

        public abstract void execute(Realm realm, Collection<Model> objects);

        @Override
        public void execute(Realm realm) {
// some how copy the obejcts async
            execute(realm, Realm.CopyObjectsFromAnotherThread(objects));
        }
    }

idok595 avatar Apr 09 '17 17:04 idok595

This probably ties into https://github.com/realm/realm-java/issues/4059

Zhuinden avatar Apr 09 '17 18:04 Zhuinden

dup to #4059

beeender avatar Apr 10 '17 08:04 beeender