Search code examples
androidrealmrealm-mobile-platform

How to read data from a Realm Object server?


I have Realm Object server and I have created a new Realm and named it as Test. I have created a new table inside it called Water.

Here are my URLS:

public static final String AUTH_URL = "https://" + INSTANCE_ADDRESS + "/auth";
public static final String REALM_URL = "realms://" + INSTANCE_ADDRESS + "/Test";

Here is my code that I am trying to read data from Water table:

SyncCredentials credentials = SyncCredentials.nickname(UUID.randomUUID().toString(), true);
SyncUser.logInAsync(credentials, AUTH_URL, new SyncUser.Callback<SyncUser>() {
  @Override
  public void onSuccess(SyncUser user) {
    SyncConfiguration syncConfiguration = new SyncConfiguration.Builder(user, Constants.REALM_URL)
        .build();
    Realm realm = Realm.getInstance(syncConfiguration);
    results = realm.where(Water.class).findAllAsync();
    callback.onSyncedUser(user.getIdentity());
  }
  @Override
  public void onError(ObjectServerError error) {
    callback.onRealmError(error.getErrorMessage());
  }
});

The issue is that I am getting always 0 results, but I am quite sure that I have data on the server since I have entered it manually.

Can somebody help?


Solution

  • You need to wait for a query to finish. Have you tried adding a RealmChangeListener?

    Async queries only return after some time.

    If that wouldn't have worked, I would have told you to use waitForInitialRemoteData() combined with Realm.getInstanceAsync.