2020-10-27

How to async load Entity in Realm?

I've initialized default realm instance and trying to do async load of entity. I have two classes Apple and AppleRealm, I have repo that should return Apple as an result (to do so it contains mapper class).

Then I tried many variants but none of them were working. I always have null as result.

One of my last tries:

public void getAppleAsync(int id, Consumer<Apple> appleConsumer, Consumer<Throwable> handler) {
        try (Realm realm = Realm.getDefaultInstance()) {
            realm.executeTransactionAsync(t -> {
                AppleRealm r = realm.where(AppleRealm.class)
                        .equalTo("id", id)
                        .findFirstAsync();
                r.addChangeListener((RealmObjectChangeListener<AppleRealm>) (realmModel, changeSet) -> {
                    appleConsumer.accept(mapper.map(realmModel));
                });
            }, () -> {}, handler::accept);
        } catch (Exception e) {
            Timber.e(e);
        }
    }

Actually, I simply don't understand how to achieve result I needed. Please help!



from Recent Questions - Stack Overflow https://ift.tt/3mrtjKA
https://ift.tt/eA8V8J

No comments:

Post a Comment