androiddatabasedebuggingrealmrealm-list

Android Realm apparently creating list, but then says it's size is 0


I'm developing an Android app which uses Realm as it's database.

This database already has some data in it when a user installs it. The problem is some of this Realm objects have a list of other objects which apparently are being created, since I saw it in the debugger, but when I try to access this list like realmObject.getList.size(); the resulting output is 0.

More specifically, I have this model:

public class Muscle extends RealmObject{

    @PrimaryKey
    private int id;

    private String name;

    private RealmList<Routine> routines;

    public Muscle(String name){
        this.id = MyApplication.MuscleID.incrementAndGet();
        this.name = name;
        this.routines = new RealmList<Routine>();
    }

    public Muscle(){}

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public RealmList<Routine> getRoutines() {
        return routines;
    }

}

And this one:

public class Routine extends RealmObject {

    @PrimaryKey
    private int id;

    @Required
    private String name;

    private RealmList<Detail> details;

    public Routine(String name){
        this.id = MyApplication.RoutineID.incrementAndGet();
        this.name = name;
        this.details = new RealmList<Detail>();
    }

    public Routine(){}

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public RealmList<Detail> getDetails() {
        return details;
    }

}

The records are being inserted in realm, since I can access and display them in my app (at least the Muscle records), and I can see in the debugger that the Routine list is also being created, but I can't access each muscles routines.

In MyApplication.java, where I save this records, the realm operations basically look like this:

public class MyApplication extends Application {

    public DateFormat df;

    public static AtomicInteger MuscleID = new AtomicInteger();
    public static AtomicInteger RoutineID = new AtomicInteger();
    public static AtomicInteger DetailID = new AtomicInteger();

    @Override
    public void onCreate() {
        super.onCreate();

        Realm.init(getApplicationContext());
        setUpRealmConfig();

        Realm realm = Realm.getDefaultInstance();
        MuscleID = getIdByTable(realm, Muscle.class);
        RoutineID = getIdByTable(realm, Routine.class);
        DetailID = getIdByTable(realm, Detail.class);

        realm.close();
    }

    private void setUpRealmConfig() {
        RealmConfiguration config = new RealmConfiguration.Builder().initialData(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {

            Muscle chest = new Muscle(getString(R.string.chest));
            realm.copyToRealmOrUpdate(chest);

            Muscle legs = new Muscle(getString(R.string.legs));
            realm.copyToRealmOrUpdate(legs);


            Routine cr1 = new Routine(getString(R.string.beginner_chest_1));
            realm.copyToRealmOrUpdate(cr1);

            chest.getRoutines().add(cr1);

            }
        })
                .deleteRealmIfMigrationNeeded()
                .build();
        Realm.setDefaultConfiguration(config);
    }

There's more but that's the relevant code. I have made sure that ids of each model increment automatically and that's working fine, all muscle objects are inserted in the database, and I can see them in my app no problem, however the routines apparently are created, since I watch the list size go up and the id increment in the debugger, but when I try to access it in my adapter like int workoutsNumber = muscle.getRoutines().size(); , workoutsNumber becomes 0.

I don't know what the problem is here. Everything seems to look fine in debugging, except for one thing I don't understand. The first thing in the debugger always is muscle = cannot find local variable 'muscle'

Here is an screenshot where you can see the objects are effectively being created, and the routine list is added to the muscle object, as well as you can see the error I mention above: debug screenshot

So why do I get 0 when doing int workoutsNumber = muscle.getRoutines().size(); if the list size should be 3?


Solution

  •         Routine cr1 = new Routine(getString(R.string.beginner_chest_1));
            realm.copyToRealmOrUpdate(cr1);
    
            chest.getRoutines().add(cr1);
    

    Should be

            Muscle chest = new Muscle(getString(R.string.chest));
            Muscle managedChestProxy = realm.copyToRealmOrUpdate(chest);
    
    
            Routine cr1 = new Routine(getString(R.string.beginner_chest_1));
            Routine managedRoutineProxy = realm.copyToRealmOrUpdate(cr1);
    
            managedChestProxy.getRoutines().add(managedRoutineProxy);