Android: Realm not creating all schema and saving data to realmSchema


#1

Hi,

I cannot figure out what is actually going on with Realm not creating the neccessary schema from my classes, as well as realm not saving data to my table. Here is a breakdown on what is really going on:

I have 6 classes which extend RealmObject, namely, Category, DialingCode, ClientInfo, Country, NotificationType, and UserData. Out of all these classes, only two appear on classToTable after my JobIntentService is done executing. This service class fetches all related data from my Web API - all calls are successful and return data.

This doesn’t seem okay to me, because when I open the generatedJava folder, and open the io.realm folder, the DefaultRealmModuleMediator does add all my classes. Furthermore, Proxies and Proxy Interfaces are generated for all my classes that extend RealmObject

class DefaultRealmModuleMediator extends RealmProxyMediator {

    private static final Set<Class<? extends RealmModel>> MODEL_CLASSES;
    static {
        Set<Class<? extends RealmModel>> modelClasses = new HashSet<Class<? extends RealmModel>>(6);
        modelClasses.add(net.koeks.koeksnet.Models.Category.class);
        modelClasses.add(net.koeks.koeksnet.Models.ClientInfo.class);
        modelClasses.add(net.koeks.koeksnet.Models.Country.class);
        modelClasses.add(net.koeks.koeksnet.Models.DialingCode.class);
        modelClasses.add(net.koeks.koeksnet.Models.NotificationType.class);
        modelClasses.add(net.koeks.koeksnet.ViewModels.UserData.class);
        MODEL_CLASSES = Collections.unmodifiableSet(modelClasses);
    }

I initialize my Realm instance in my application class which extends MultiDexApplication - yeah, I am supporting KitKat devices and upwards.

public class KoeksNetApplication extends Application {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
    @Override
    public void onCreate() {
        super.onCreate();
        Fabric fabric = new Fabric.Builder(this).kits(new Crashlytics()).debuggable(true).build();
        Fabric.with(fabric);
        Realm.init(this);
        RealmConfiguration configuration = new RealmConfiguration.Builder().name("KoeksDotNet.realm").schemaVersion(0).build();
        Realm.setDefaultConfiguration(configuration);
    }

When the app launches, I run a service which is to populate Realm with the neccessary data. Here is a snippet of that class:

public class ApplicationStartupService extends JobIntentService {
    @Override
    protected void onHandleWork(@NonNull Intent intent) {
        final Intent toApp = new Intent("net.koeks.koeksnet.APPLICATION_STARTUP_SERVICE");
        OkHttpClient client = loadAnchorCertificate(toApp);
        if (client == null) {
            toApp.putExtra("DidError", true);
            toApp.putExtra("Message", "Unable to start the loading process");
            sendBroadcast(toApp);
            return;
        }
        Retrofit retrofit = new Retrofit.Builder().baseUrl(getResources().getString(R.string.koeksdotnetapi)).addConverterFactory(GsonConverterFactory.create()).client(client).build();
        KoeksDotNetService service = retrofit.create(KoeksDotNetService.class);
        try {
            Response<ListResponse<Country>> countryCall = service.getCountries().execute();
            if (countryCall.isSuccessful()) {
                final List<Country> countries = countryCall.body().getModel();
                if (countries != null) {
                    Realm.getDefaultInstance().executeTransaction(new Realm.Transaction() {
                        @Override
                        public void execute(Realm realm) {
                            realm.copyToRealmOrUpdate(countries);
                        }
                    });
                }
            } else {
                String errorData = "Status Code: "+countryCall.code() + countryCall.errorBody().string();
                Crashlytics.logException(new Throwable(errorData));
                toApp.putExtra("DidError", true);
                toApp.putExtra("Message", countryCall.message());
                Realm.getDefaultInstance().executeTransaction(new Realm.Transaction() {
                    @Override
                    public void execute(Realm realm) {
                        realm.deleteAll();
                    }
                });
                sendBroadcast(toApp);
                return;
            }
            Response<ListResponse<DialingCode>> dialingCodeCall = service.getDialingCodes().execute();
            if (dialingCodeCall.isSuccessful()) {
                final List<DialingCode> dialingCodes = dialingCodeCall.body().getModel();
                if (dialingCodes != null) {
                    Realm.getDefaultInstance().executeTransaction(new Realm.Transaction() {
                        @Override
                        public void execute(Realm realm) {
                            realm.copyToRealmOrUpdate(dialingCodes);
                        }
                    });
                }
            } else {
                Crashlytics.logException(new Throwable(dialingCodeCall.errorBody().string()));
                toApp.putExtra("DidError", true);
                toApp.putExtra("Message", dialingCodeCall.message());
                Realm.getDefaultInstance().executeTransaction(new Realm.Transaction() {
                    @Override
                    public void execute(Realm realm) {
                        realm.deleteAll();
                    }
                });
                sendBroadcast(toApp);
                return;

            }

All calls to my API are successful, and every call does return data. My model classes all extend RealmObject. Could someone please help me on this one - suggest other things I can try? I am using the latest version of Android Studio (3.2.1) and the latest Realm (5.7.0). Please help