lmdic lmdic - 6 months ago 29
Android Question

Android: How to handle when Realm finishes sync?

I want to start (on background) sync with Realm Object Server when launching my Android app. And after data was successfully downloaded I want to show toast.

How I can do this? What method do I need to use?

Answer Source

I have not tried this but it should work.

private void setRealmDefaultConfiguration(SyncUser syncUser, String realmURL) {
    SyncConfiguration config = new SyncConfiguration.Builder(syncUser, realmURL)

public abstract class BaseActivity extends Activity {
    private static boolean firstInit = true;

    protected Realm realm = null;

    public void onCreate(Bundle bundle) {
        final boolean shouldShowToast;
        if(firstInit) {
            firstInit = false;
            shouldShowToast = true;
        } else {
            shouldShowToast = false;
        Realm.getInstanceAsync(Realm.getDefaultConfiguration(), new Realm.Callback() {
            public void onSuccess(Realm realm) {
                if(isChangingConfigurations() || isFinishing()) {
                } else {
                    BaseActivity.this.realm = realm;
                if(shouldShowToast) {
                    Toast.makeText(BaseActivity.this, R.string.data.loaded, Toast.LENGTH_LONG).show();

            public void onError(Throwable throwable) {
                // boop

    protected void onRealmLoaded(Realm realm) {
        // override this if needed
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download