Realm-java: Question: how to change @Ignore field data?

Created on 4 Mar 2015  路  12Comments  路  Source: realm/realm-java

Please observe code below. Why when I am changing field marked as @Ignore it remains with default value. E.g. dog.isSick is always false.

class Dog extends RealmObject {
    @Ignore
    boolean isSick;
}

RealmResults<Dog> allObjects = realm.allObjects(Dog.class);
for (Dog dog : allObjects) {
    dog.isSick = true;
}

for (Dog dog : allObjects) {
    dog.isSick // false
}

T-Help

Most helpful comment

@ahmadalibaloch be honest, it is something difficult to implement according to the Realm's current concept. The managed RealmObject is just a frond end of Realm underlying engine's native object, and those native objects are actually mmap to the realm physical file on the disk. Which means when setter called, the relevant data will be written to the disk ... So I think even for a long term, you need to use that alternative way i mentioned above ...

But for the idea of in-mem fields you talked about, i think it is quite useful and would be great to have an issue to track it, but we may take it as a low priority feature... sorry .

All 12 comments

Hi @dmytrodanylyk
The reason that doesn't work is that the ignored fields are completely ignored by Realm, and each time you make a Realmresults.get(), it actually creates a new object. So how you iterate there actually create two independent objects.

If you want to set a ignored field and use it, you will have to keep an reference to that object.

@cmelchior ok, thanks.

My use case is following: in ListView you can check some items, so I need to save some boolean value to remember that. Your answer means I need to create separate list of boolean.

How do I get a reference to RealmResults.get(0) or when actually do I need to store it somewhere, later will be difficult and not recommended because I have to pass these results to other methods for processing logic.

@ahmadalibaloch You need to store it somewhere.

MyObject obj = results.get(0); // you need to hold a ref to this obj
obj.setIgnoredField(1);
obj.getIgnoredField(); // == 1

obj = results.get(0); // Here will create a new MyObject and return
obj.getIgnoredField(); // != 1 // it won't be 1 anymore since it is a new object.

Thank you for being quick. You solution is an alternative. So there is no way to update @ignored fields in RealmResults. Right? In this case @Ignore field loses the purpose of existence to me :-) its is very basic usage for them . I need to pass these results to several other methods for processing.

@ahmadalibaloch Well, if you want to obtain data through your results from the Realm, then the data you want to access must be persisted to the Realm.

@Ignore makes fields not be persisted to the Realm.

The solution depends on what kind of data you have in your object that you explicitly @Ignore and therefore don't persist.

I think @ignore fields should not be part of realm. In the case of RealmResults update, we have two options, they should remain unchanged or change to default value. But if there is no change it is clear, RealmResults should persist the @ignore for that session of results (not to database but list).
What do you suggest if I should create another issue for this?

@ahmadalibaloch be honest, it is something difficult to implement according to the Realm's current concept. The managed RealmObject is just a frond end of Realm underlying engine's native object, and those native objects are actually mmap to the realm physical file on the disk. Which means when setter called, the relevant data will be written to the disk ... So I think even for a long term, you need to use that alternative way i mentioned above ...

But for the idea of in-mem fields you talked about, i think it is quite useful and would be great to have an issue to track it, but we may take it as a low priority feature... sorry .

I just stepped into the same problem.
I want some attributes to be ignored, so Realm won't persist them. But I need them in memory.
I get those from another source, so it doesn't make much sense to persist them in Realm.

Just put it in some map somewhere where the key is the primary key of the realm object

@Zhuinden yeah but if ignore fields would get updated in memory, then it will become really easy for us, Realm should change this feature 馃槩

RealmResults returns a new instance of item each time you access it, so there is nothing to share in-memory :|

Was this page helpful?
0 / 5 - 0 ratings