Realm doesn't use bridge tables or explicit joins to define relationships as you would in a relational database. Realm handles relationships through embedded objects or reference properties to other Realm objects. You read from and write to these properties directly. This makes querying relationships as performant as querying against any other property.
Tipos de relaciones
Realm admite relaciones uno a uno, uno a muchos e inversas. Realm también ofrece un tipo especial de objeto, denominado objeto incrustado, que es conceptualmente similar a una relación, pero ofrece restricciones adicionales.
To-One Relationship
Una relación uno a uno significa que un objeto se relaciona con otro objeto. Definís una relación uno a uno para un tipo de objeto Realm en su Modelo de objeto. Especifica una propiedad cuyo tipo sea el tipo de objeto de Realm relacionado. Por ejemplo, un perro podría tener una relación uno a uno con su juguete favorito.
Relación de muchos
Una relación de uno a muchos significa que un objeto se relaciona con más de otro objeto. En Realm, una relación de uno a muchos es una lista de referencias a otros objetos. Por ejemplo, una persona podría tener muchos perros.
You can represent a to-many relationship between two Realm types as a list, map, or a set. Lists, maps, and sets are mutable: within a write transaction, you can add and remove elements to and from these collection types. Lists, maps, and sets are not associated with a query and are declared as a property of the object model.
Relación inversa
Las definiciones de relación en Realm son unidireccionales. Una relación inversa vincula un objeto de vuelta a un objeto que lo refiere.
An inverse relationship property is an automatic backlink relationship. Realm automatically updates implicit relationships whenever an object is added or removed in a corresponding to-many list or to-one relationship property. You cannot manually set the value of an inverse relationship property.
Declare Relationship Properties
Tip
Alternativamente, puedes Define tus relaciones en tu aplicación App Services.
Definir una relación de uno a uno
Una relación a uno asigna una propiedad a una sola instancia de otro tipo de objeto. Por ejemplo, se puede modelar un perro que tenga como máximo un juguete favorito como una relación a uno.
Setting a relationship field to null removes the connection between objects. Realm does not delete the referenced object, though, unless it is an embedded object.
Importante
To-one relationships must be optional
When you declare a to-one relationship in your object model, it must be an optional property. If you try to make a to-one relationship required, Realm throws an exception at runtime.
struct FavoriteToy { realm::primary_key<realm::uuid> _id; std::string name; }; REALM_SCHEMA(FavoriteToy, _id, name) struct Dog { realm::primary_key<realm::uuid> _id; std::string name; int64_t age; // Define a relationship as a link to another SDK object FavoriteToy* favoriteToy; }; REALM_SCHEMA(Dog, _id, name, age, favoriteToy)
Definir una relación de varios-a-varios
A to-many relationship maps one property to zero or more instances of another object type. For example, you can model a company having any number of employees as a to-many relationship.
struct Company { int64_t _id; std::string name; // To-many relationships are a list, represented here as a // vector container whose value type is the SDK object // type that the list field links to. std::vector<Employee*> employees; }; REALM_SCHEMA(Company, _id, name, employees)
Definir una relación inversa
Para definir una relación inversa, utilice linking_objects En su modelo de objetos. La definición linking_objects especifica el tipo de objeto y el nombre de la propiedad de la relación que invierte.
In this example, we define a Person having a to-one relationship with a Dog. The Dog has an inverse relationship to any Person objects through its owners property.
struct Dog; struct Person { realm::primary_key<int64_t> _id; std::string name; int64_t age = 0; Dog* dog; }; REALM_SCHEMA(Person, _id, name, age, dog) struct Dog { realm::primary_key<int64_t> _id; std::string name; int64_t age = 0; linking_objects<&Person::dog> owners; }; REALM_SCHEMA(Dog, _id, name, age, owners)