Help
- Programming problems? Ask StackOverflow, we will actively monitor and answer questions!
- Submit a bug? Open a question in our repo. If you can, please let us know the realm version, full log, realm file and the item displaying the problem.
- Feature request? Open a question in our repo. Tell us what this feature should do and why it is needed.
- Want to know what we're going to update next? Take a look at our changelog. The log shows the latest additions and changes that we plan to release recently, and the history of realm.
Model
The Realm model class is a subclass of Realmobject
Public class User extends realmobject { PrivateString name;Private intAge@Ignore Private intSessionId;//Standard getters & setters generated by your IDE ... PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } Public int Getage() {returnAge } Public void Setage(intAge) { This. Age = Age; } Public int GetSessionID() {returnSessionId; } Public void Setsessionid(intSESSIONID) { This. sessionId = SessionId; }}
A realm model class also supports the Public,protected,private field and custom methods
publicclass User extends RealmObject { public String name; publicbooleanhasLongName() { return7; } @Override publicbooleanequals(Object o) { // Custom equals comparison }}
Field type
Realm supports the following field types: Boolean,byte,short,int,long,float,double,string,date and Byte []. Integer types Byte,short,int and long are mapped to the same type in realm (in fact, long). In addition, Realmobject and realmlist
Required fields and Null values
In some cases, NULL is not the appropriate value for the field. @Required annotations can be used to tell realm to enforce checks to suppress null values. Only Boolean, byte, short Integer, Integer, Long integer, float, double, string, byte [] and date can be annotated as required. When other types of fields have @required annotations, compilation will fail. Fields of the original type and realmlist type are required by default. Fields of type Realmobject are always nullable.
Ignore properties
Annotation @ignore means that a field should not be persisted to disk. Ignoring fields is useful if your input contains more fields than the model, and you do not want to have many special cases to work with these unused data fields.
Automatically update objects
Realmobjects is real-time and automatically updates the view to the underlying data, which means that the object does not have to be refreshed. Modifying the object that affects the query is immediately reflected in the results.
Realm. Executetransaction(New Realm. Transaction() {@Override public void execute (Realm realm) {Dog Mydog = Realm. CreateObject(Dog. Class);Mydog. SetName("Fido");Mydog. Setage(1);}});Dog Mydog = Realm. Where(Dog. Class). Equalto("Age",1). FindFirst();Realm. Executetransaction(New Realm. Transaction() {@Override public void execute (Realm realm) {Dog mypuppy = Realm. Where(Dog. Class). Equalto("Age",1). FindFirst();Mypuppy. Setage(2);}});Mydog. Getage();//= 2
This attribute of all realmobject and realmresults not only keeps realm fast and efficient, but it allows you to make your code simpler and more flexible. For example, if your activity or fragment relies on a specific realmobject or realmresults instance, you do not have to worry about refreshing or retrieving it before updating the UI.
You can subscribe to the realm notification to see when the realm data is updated to indicate when the app's UI should refresh
Indexed properties
Annotation @index adds a search index to the field. This will make the insertion slower, the data file larger, but the query faster. Therefore, it is recommended that you only add indexes when you are optimizing read performance for specific situations. We support indexing: String,byte,short,int,long,boolean and Date fields.
Primary key
To promote a field as a primary key, use note @primarykey, where the field type must be either a string or an integer (Byte,short,int or long) and its encapsulated class (Byte,short,integer and Long). You cannot use multiple fields (composite keys) as primary keys. Using a string field as the primary key means that the field is indexed (note @primarykey implicitly sets the annotation @index).
With the primary key, we can use the Copytorealmorupdate () method to find an existing object using this primary key, update it if it is found, or create a new object if it is not found. When you call Copytorealmorupdate () on a class that does not have a primary key, an exception is thrown.
The use of primary keys has an impact on performance. Creating and updating objects is a bit slow, and queries are expected to be a little faster. Because the performance changes depend on the size of the dataset, it is difficult to give a definite value.
When Realm.createobject () is called, it returns a new object and all fields are set to their default values. In this case, there may be a conflict with an existing object that has a default value for the primary key field. To avoid this, we recommend that you create an unmanaged object, set the value of the field, and then copy it to realm by using the Copytorealm () method.
Final MyObject obj = new MyObject (); obj.setid; Obj.setname ("Fish"); Realm.executetransaction (New realm.transaction () {@Override public void execute (Realm realm) {//This would Create A new object in Realm or throw a exception if the//OBJEC T already exists (same primary key)//Realm.copytorealm (obj);This would Update an existing object with the same primary key // or Crea Te A new object if a object with no primary Key = A Realm.copytorealmorupdate (obj);}});
For string and encapsulated integers (Byte,short,integer and long), the primary key can have a null value unless @primarykey is used with @required.
Custom Objects
We can almost use realmobjects as Pojo. Inherited from Realmobject, we can set the public field and use simple assignment instead of setter and getter. An example of such a model class is
publicclass Dog extends RealmObject { public String name; publicint age;}
You can use dog like any other class. In order to create managed dog objects in realm, you can use the CreateObject () or Copytorealm () method.
realm.executeTransaction(new Realm.Transaction() { @Overrride publicvoidexecute(Realm realm) { Dog dog = realm.createObject(Dog.class); "Fido"; dog.age 5; }};
You can add logic to your setters and getters if you better meet your needs. This can be useful if you want to validate values before storing them in realm. In addition, you can easily add custom methods to Realmobjects.
Limit
Final,transient and volatile fields are not currently supported. This is primarily to avoid the difference between how the object is managed by realm or the behavior that is not managed.
The REALM model class does not allow the inheritance of any object other than Realmobject. If you want to declare a constructor, the default constructor (a constructor with no arguments) must always be empty. The reason is that the default constructor invokes a method that assumes the existence of a realm instance. However, this instance was not created before the creator returned. You can also add constructors yourself.
Realm Model Interface
Another way to inherit Realmobject is to implement the Realmmodel interface and add @realmclass annotations
@RealmClasspublicclass User implements RealmModel {}
All methods available on Realmobject can be used in a static way.
// With RealmObjectuser.isValid();user.addChangeListener(listener);// With RealmModelRealmObject.isValid(user);RealmObject.addChangeListener(user, listener);Relationships
Original link
https://realm.io/docs/java/latest/#getting-help
<android Open Source Library > Realm for android~ Getting Help and Models