0

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())
        }
    });
  • At a high level, this is generally done by observing data that you want to be notified of changes for. Then once a change is made, and event will be delivered to your app where it can react to those changes; update the UI for example. Do you have some code that isn't working? Please review the following two guides on asking questions: [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Jay Nov 19 '22 at 14:28
  • Thanks for the comment. My question is that if a field gets updated within a Realm Object then do I have to listen to the Realm Object that the field lies in or can I specifically observe any changes for a particular field? I am sort of new to MongoDB and Realm so getting confused to perform CRUD. Thanks for the help! – Vaibhav Patel Nov 19 '22 at 19:09
  • 1
    I think your question is covered in the documentation, no? The most granular observe is on an object itself - if any of the objects fields (properties) change, the app will be notified of that change. That event will then contain details about what changed within the the object. There's a pretty good example in the Documentation [React To Changes](https://www.mongodb.com/docs/realm/sdk/java/examples/react-to-changes/#register-an-object-change-listener) where you can output which object field(s) changed. – Jay Nov 20 '22 at 13:39

0 Answers0