Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Practical use of @Ignore in Realm?

Tags:

android

realm

I've been trying to add Realm in my Android app. Their docs are pretty well explained & easy to follow. But it fails to explain this one particular area. I'm unable to figure out the practical use for the @Ignore annotation. I know that fields under this annotation are not persisted.

Can someone please share a few use cases. Also I wanted to know the scope of such fields. I mean, if I set an @Ignore field to some value, would that value be available to the other classes in my app for that particular launch session. If yes, then how do we access it? If no (which I guess is the case), then why do we need such a field anyway?

I've searched here and on web but couldn't find the relevant information. If out of my ignorance, I've missed upon some resource, please guide me to it.

Thanks.

like image 844
Anjani Avatar asked Nov 29 '15 10:11

Anjani


4 Answers

Accordingly to the official documentation (see https://realm.io/docs/java/latest/) @Ignore is useful in two cases:

  1. When you use GSON integration and your JSON contains more data than you want to store, but you still would like to parse it, and use right after.

  2. You can't create custom getters and setter in classes extending RealmObject, since they are going to be overridden. But in case you want to have some custom logic anyway, ignored fields can be used as a hack to do that, because Realm doesn't override their getter & setters. Example:

    package io.realm.entities;
    
    import io.realm.RealmObject;
    import io.realm.annotations.Ignore;
    
    public class StringOnly extends RealmObject {
    
        private String name;
    
        @Ignore
        private String kingName;
    
        // custom setter
        public void setKingName(String kingName) { setName("King " + kingName); }
    
        // custom getter
        public String getKingName() { return getName(); }
    
        // setter and getter for 'name'
    }
    

Ignored fields are accessible only from the object they were set in (same as with regular objects in Java).

UPDATE: As the @The-null-Pointer- pointed out in the comments the second point is out of date. Realm now allows having custom getters and setters in Realm models.

like image 77
Roberto Artiles Astelarra Avatar answered Nov 16 '22 03:11

Roberto Artiles Astelarra


Here's a couple of real-world use cases:

1 - Get user's fullname:

public class User extends RealmObject {

private String first;
private String last;

@Ignore
private String fullName;

public String getFullName() {
    return getFirst() + " " + getLast();
}

Get JSON representation of object:

public class User extends RealmObject {

private String first;
private String last;

@Ignore
private JSONObject Json;

public JSONObject getJson() {
     try {
        JSONObject dict = new JSONObject();

        dict.put("first", getFirst());
        dict.put("last", getLast());
        return dict;
    } catch (JSONException e) {
        // log the exception
    }

    return null;
}
like image 39
northernman Avatar answered Nov 16 '22 03:11

northernman


I've found it useful to define field names for when I am querying. For example

User.java

public class User extends RealmObject {
    @Index
    public String name;

    @Ignore
    public static final String NAME = "name";
}

And then later on I can do something like:

realm.where(User.class).equalTo(User.NAME, "John").findFirst();

This way if the schema changes from say name to id I don't have to hunt down every occurrence of "name".

like image 39
Bill Avatar answered Nov 16 '22 03:11

Bill


Please see the the official documentation about @Ignore annotation:

The annotation @Ignore implies that a field should not be persisted to disk. Ignored fields are useful if your input contains more fields than your model, and you don’t wish to have many special cases for handling these unused data fields.

like image 43
valerybodak Avatar answered Nov 16 '22 02:11

valerybodak