Practical use of @Ignore in the kingdom?

I am trying to add Realm to my android app. Their documents are pretty well explained and easy to follow. But he does not explain this particular area. I cannot understand the practical use of @Ignore annotation. I know that the fields under this annotation are not saved.

Someone can share several use cases. I also wanted to know the volume of such fields. I mean, if I set the @Ignore field to some value, whether this value will be available to other classes in my application for this particular launch session. If so, how do we appeal to him? If not (which I think is), then why do we need such a field anyway?

I searched here and on the Internet, but could not find the relevant information. If I missed a resource from my ignorance, please direct me to it.

Thanks.

+7
android realm
source share
4 answers

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

  • When you use GSON integration and your JSON contains more data than you want to save, but you still want to analyze and use it right after.

  • You cannot create custom getters and setters in classes that extend RealmObject, as they will be overridden. But in any case, if you want to have some kind of user logic, ignored fields can be used to hack this, because Realm does not override their recipients and 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 in which they were installed (the same as with regular objects in Java).

UPDATE: As pointed out by @ The-null-Pointer, in the comments the second paragraph is deprecated. Realm now allows you to create custom getters and setters in Realm models.

+8
source share

Here are a couple of real-world use cases:

1 - Get the full username:

 public class User extends RealmObject { private String first; private String last; @Ignore private String fullName; public String getFullName() { return getFirst() + " " + getLast(); } 

Get a view of the JSON 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; } 
+3
source share

I found it useful to define field names for queries. for example

User.java

 public class User extends RealmObject { @Index public String name; @Ignore public static final String NAME = "name"; } 

And then later I can do something like:

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

That way, if the schema changes from say name to id , I don't need to track every occurrence of "name" .

+1
source share

Please check out the official @Ignore annotation documentation :

The @Ignore annotation implies that the field should not be saved to disk. Ignored fields are useful if your input contains more fields than your model, and you do not want to have many special cases for handling these unused data fields.

0
source share

All Articles