Dennis Zinkovski
Dennis Zinkovski

Reputation: 1851

How to understand Realm's behavior on android?

I try to go deeper in the Realm, I'm using it last few days in our project and can't understand some strange behavior as for me, in the documentation I doesn't find any useful information, I looked in their samples apps but still not enough information, so can anyone explain me. Why model returned by Realm are empty, in browser it shows correct data, but not in debug mode.realm debug

According to the sample "Intro example", here is snippet:

private void basicCRUD(Realm realm) {
    showStatus("Perform basic Create/Read/Update/Delete (CRUD) operations...");

    // All writes must be wrapped in a transaction to facilitate safe multi threading
    realm.beginTransaction();

    // Add a person
    Person person = realm.createObject(Person.class);
    person.setId(1);
    person.setName("Young Person");
    person.setAge(14);

    // When the transaction is committed, all changes a synced to disk.
    realm.commitTransaction();

    // Find the first person (no query conditions) and read a field
    person = realm.where(Person.class).findFirst();
    showStatus(person.getName() + ":" + person.getAge());

    // Update person in a transaction
    realm.beginTransaction();
    person.setName("Senior Person");
    person.setAge(99);
    showStatus(person.getName() + " got older: " + person.getAge());
    realm.commitTransaction();

    // Delete all persons
    realm.beginTransaction();
    realm.allObjects(Person.class).clear();
    realm.commitTransaction();
}

I spent all day. My main problem next, I have one activity A within one fragment with adapter(lets says that has model of the rabbit and displays his 4 carrots), after click "+" on the fragment I replace with a new fragment where I create 3 carrots, then after popBackStack() I wont add this 3 carrots to the previous 4.

How I do it: Activity(has instance of Rabbit which has 4 carrots, saved in Realm)-->Fragment A (4 carrots)-->Fragment B(get instance from Realm, carrots in empty, why? I wont add 3 carrot and save to Realm my rabbit)-->popBackStack-->OnCreateView Framgent A(get Rabbit from Realm, adapter try to display data but carrots is empty. Go home).

Upvotes: 0

Views: 138

Answers (1)

Christian Melchior
Christian Melchior

Reputation: 20126

Realm is a zero-copy database. This means that unlike ORM's that copy all their data to the Java heap before you can access it, we store all our data in native memory. You can think of RealmObjects as type-safe cursor objects. This also means that the IntelliJ debugger will not show the correct values, as it shows the values in the Java heap. We uses proxy classes to override the behaviour of your getters and setters (in your screen shoot: PersonRealmProxy), which is why using the getX() methods will return the correct values.

Regarding the second question, then it would be easier to create an issue on GitHub so we can ask for more details: https://github.com/realm/realm-java/issues

Upvotes: 4

Related Questions