How to dynamically update ui using MongoDB realm

120 Views Asked by At

I am new to working with Realm and MongoDB and I was wondering if there is a better way of updating the UI.

I want the UI to be updated for the fields that have been changed is there a way to identify that so that I can avoid updating fields that don't need any updates?

My code currently to update a particular field:

SyncConfiguration configuration = new SyncConfiguration.Builder(app.currentUser())
            .initialSubscriptions(new SyncConfiguration.InitialFlexibleSyncSubscriptions() {
                @Override
                public void configure(Realm realm, MutableSubscriptionSet subscriptions) {
                    // Add a subscription with a name
                    subscriptions.addOrUpdate(Subscription.create("userQuery",
                            realm.where(user.class)
                                    .equalTo("uid", app.currentUser().getId())
                    ));
                }
            })
            .build();

    Realm realm = Realm.getInstance(configuration);

    currUserInfo = realm.where(user.class)
            .equalTo("uid", app.currentUser().getId()).findFirst();
    
    currUserInfo.addChangeListener(new RealmChangeListener<RealmModel>() {
        @Override
        public void onChange(RealmModel realmModel) {
            greetingText.setText("Hello, "
            + currUserInfo.getFirstName()
            + " " + currUserInfo.getLastName());
            firstNameText.setText(currUserInfo.getFirstName())
            lastNameText.setText(currUserInfo.getLastName())
        }
    });
0

There are 0 best solutions below