Realm Java 3.4 - Reverse Relationship Queries and Sync Progress Listeners

We’re releasing version 3.4 of Realm Java today, and with it introducing reverse relationship queries and sync progress listeners. Read on for all the details.

Reverse Relationship Queries

In Realm Java 3.1 we added beta support for reverse relationships using the @LinkingObjects annotation. In 3.4 we are making it possible to query those relationships as you would with a normal RealmList or RealmObject reference:

public class Person extends RealmObject {
   public String name;
   public Dog dog;
}
 
public class Dog extends RealmObject {
   public String name;
   @LinkingObjects
   public final RealmResults<Person> owners = null;
}
 
// Query for owner as you would a RealmList
Person owner = realm.where(Dog.class).equalTo("owners.name", "Jane").findFirst();

Queries on @LinkingObjects work the same way as Link Queries on RealmLists and with this change we are considering @LinkingObjects stable, and are moving them out of beta.

Sync Progress Listeners

Realm Mobile Platform offers a true offline-first experience, where changes can be applied immediately to the local Realm irrelevant of the network conditions. In the background, synchronization happens automatically, and when the network is available, changes quickly propagate across devices and the server. This enables the realtime, collaborative experiences unique to Realm.

There are times, however, when it’s useful to know how much data is left to transfer. Perhaps on the first launch of your app, you’d like to make sure a sizable amount of the data is available before presenting your user interface. At other times, your app will simply benefit from showing when data is synchronizing, by presenting a progress bar or activity indicator.

This is why we’ve added APIs to monitor sync progress by registering ProgressListeners on the SyncSession.

Realm realm = Realm.getInstance(config)
writeData(realm);
SyncSession session = SyncManager.getSession(config);
 
// Show progress bar while waiting for an image you just wrote to the Realm
// to be uploaded
showProgressBar();
session.addUploadProgressListener(ProgressMode.CURRENT_CHANGES, new ProgressListener() {
   @Override
   public void onChange(Progress progress) {
       if (progress.isTransferComplete()) {
           hideProgressBar();
           session.removeProgressListener(this);
       } else {
           updateProgressBar(progress.getFractionTransferred());
       }
   }
});
 
// Show a UI hint as long as you are downloading data from the remote server
session.addDownloadProgressListener(ProgressMode.INDEFINITELY, new ProgressListener() {
   @Override
   public void onChange(Progress progress) {
       showDownloadingChanges(!progress.isTransferComplete());
   }
});

Note, that currently progress listeners can only be registered if a Realm instance is open.

See the CHANGELOG for the full list of changes.


Thanks for reading. Now go forth and build amazing apps with Realm! As always, we’re around on Stack Overflow, GitHub, or Twitter.