This page describes the Device Sync data model and how it's used to map data from the App Services schema used by Device Sync to the Realm schema used by the Kotlin SDK in the client.
This page does not:
Explica cómo definir un modelo de objeto de Realm en la aplicación cliente. Para aprender cómo hacerlo, consulte Definir un Modelo de objeto Realm - Kotlin SDK.
Explain how to set up Device Sync on a client app. To learn how, refer to Add Device Sync to an App - Kotlin SDK.
Para obtener una explicación detallada de la sincronización del dispositivo, consulte Comience a utilizar Atlas Device Sync en la documentación de Atlas App Services.
Tip
The table in the Schema Types Mapping List section on this page provides a quick reference for supported data types and how they map between Realm and App Services. For a more comprehensive resource, refer to Data Model Mapping in the App Services documentation.
Define un Device Sync modelo de datos
The Atlas Device Sync data model is defined by the following schemas, which allows Device Sync to map your data between the client and Atlas:
Esquema Realm: el Modelo de objeto del lado del cliente en tu aplicación que define tus datos como clases de Kotlin usando el Kotlin SDK.
App Services schema: the server-side schema in Atlas App Services that defines your data in BSON. For more information, refer to Schemas in the App Services documentation.
Device Sync utiliza estos esquemas para validar y convertir objetos entre el formato Kotlin y BSON al sincronizar datos. Cuando sincronizas datos desde el cliente, Device Sync convierte automáticamente los tipos de datos de Realm Kotlin a BSON. Luego, cuando el dispositivo cliente sincroniza datos de Atlas a través de Device Sync, el SDK convierte los datos BSON nuevamente en objetos Kotlin.
To use Device Sync, you must define a Realm schema and an App Services schema, and both schemas must be consistent with each other.
You can define your schema in the client app or in Atlas first, depending on your preference and use case. You can then generate a corresponding schema with matching object models.
Generate a Schema from the Client App
If you are developing a new client application, you likely want to iterate on the data model in the client app. After you define an object model directly in your client app code, you can enable Development Mode in App Services to generate a matching App Services schema automatically.
Modo de desarrollo es una configuración que permite que Device Sync infiera y actualice esquemas a partir de modelos de datos del lado del cliente cuando sincronizas datos desde el cliente. Para obtener más información, consulta Modo de desarrollo en la documentación de App Services.
Generate a Schema with Existing Atlas Data
If you are developing a client application that works with data that already exists in Atlas, you can generate a schema from that data, and then generate SDK object models to use in your Kotlin client app. To learn more, refer to Sync Data in Atlas with a Client Application in the App Services documentation.
Device Sync Requirements
There are a few requirements to successfully sync objects. As outlined earlier, you must have matching Realm and App Services schemas that contain the objects that you want to sync.
Además, Device Sync requiere que:
Sus modelos de objetos deben tener un campo de clave principal llamado
_idPuede ser de tipoString,IntoObjectId. Device Sync lo utiliza para identificar objetos en las colecciones de Atlas.Si un objeto no tiene definido un campo
_id, Realm mostrará el siguiente error de validación de esquema:There must be a primary key property named '_id' on a synchronized Realm.Cada clase debe tener al menos un campos consultables.
When Development Mode is enabled, fields that you include in client subscription queries are automatically added as queryable fields in Atlas. For more information on configuring subscriptions with the Kotlin SDK, refer to Subscriptions Overview.
Mapeo de Objetos Realm
Realm objects are the uniquely named instances of Kotlin classes defined in your Realm schema that determine the properties and relationships for objects of that type.
App Services maps Realm objects to Atlas in the following ways:
Realm object names map to Atlas collections in your linked Device Sync data source. Note the following:
Cuando el modo de desarrollo está habilitado, App Services crea automáticamente una colección y esquema para cada nuevo tipo de objeto Realm que sincronices.
Embedded objects are not stored in their own collection in Atlas. This is because they cannot exist outside of their parent object type. Refer to the Embedded Objects section on this page for more information.
The Realm object schema maps to an App Services schema within its appropriate collection.
En el siguiente ejemplo, tenemos objetos Frog y Pond con propiedades básicas definidas en nuestro esquema de Realm:
// Maps to `Frog` collection class Frog : RealmObject { var _id: ObjectId = ObjectId() var name: String = "" var age: Int? = null } // Maps to `Pond` collection class Pond : RealmObject { var _id: ObjectId = ObjectId() var name: String = "" }
En Atlas, podemos ver cómo estos tipos de objetos se mapean a los respectivos esquemas de App Services -- cada objeto se asocia con su respectiva colección. También vemos ejemplos de objetos rana y estanque que se ajustan a este modelo de datos:
{ "title": "Frog", "type": "object", "required": [ "_id", "name" ], "properties": { "_id": { "bsonType": "objectId" }, "age": { "bsonType": "long" }, "name": { "bsonType": "string" } } }
{ "title": "Pond", "type": "object", "required": [ "_id", "name" ], "properties": { "_id": { "bsonType": "objectId" }, "name": { "bsonType": "string" } } }
{ "_id": ObjectId("5af712eff26b29dc5c51c60f"), "name": "Kermit", "age": 42 }
{ "_id": ObjectId("5af714eff24b294c5251cf04"), "name": "Kermit's Pond" }
Realm Relationship Mapping
Your Realm object model might include relationships between objects. There are two primary types of relationships:
To-one relationship: an object is related in a specific way to no more than one other Realm object.
To-many relationship: an object is related in a specific way to multiple Realm objects.
Las relaciones se mapean por propiedades que hacen referencia a la llave primariade otro objeto Realm.
Para obtener más información sobre la modelización de relaciones en un esquema de Servicios de aplicación, consulta Relaciones en la documentación de Servicios de aplicación.
Mapear relaciones uno a uno
Una relación uno a uno (to-one relationship) asigna una propiedad a una sola instancia de otro objeto Realm. Las relaciones uno a uno deben ser opcionales. Para obtener más información sobre cómo se definen las relaciones uno a uno en el Kotlin SDK, consulta Definir una propiedad de relación uno a uno.
Using the objects in the example above, consider a case where a Frog can have one favorite pond. We can add a favoritePond property to our Frog model that is an optional link to a Pond object.
class Frog : RealmObject { var _id: ObjectId = ObjectId() var name: String = "" var age: Int? = null // To-one relationship (MUST be optional) var favoritePond: Pond? = null }
In the App Services schema, we see the new property translates to a field favoritePond:
The field is not in the
requiredarray because it is an optional property.Su tipo es un
objectIdque enlaza a un objetoPondespecífico en la colecciónPonddistinta. Esto se debe a que definimos la llave primaria en nuestro modelo dePondcomo unobjectId.
The Pond schema doesn't change. Because this is a to-one relationship, it's a one-way relationship; the Pond has no relationship back to Frog.
{ "title": "Frog", "type": "object", "required": [ "_id", "name" ], "properties": { "_id": { "bsonType": "objectId" }, "age": { "bsonType": "long" }, "favoritePond": { "bsonType": "objectId" }, "name": { "bsonType": "string" } } }
To-Many Relationship
Una relación de muchos a muchos asigna una propiedad a cero o más instancias de otro objeto Realm. Para obtener más información sobre cómo se definen las relaciones muchos a muchos en Kotlin SDK, consulta Definir una Propiedad de Relación de Muchos.
Consider another case where a Frog can have many favorite ponds instead of only one. We add a favoritePonds property to our Frog model that is a list of Pond objects. If the frog has no favorite ponds, this is an empty list. As the frog gets favorite ponds, we can create new Pond objects and append them to the frog's favoritePonds list.
class Frog : RealmObject { var _id: ObjectId = ObjectId() var name: String = "" var age: Int? = null // To-many relationship (can have many ponds) var favoritePonds: RealmList<Pond> = realmListOf() }
In the App Services schema, we see the new property translates to a favoritePonds field that contains all of the Pond objects related to the Frog object:
The field is not in the
requiredarray because it is an optional property.The type of this field is an array of type
objectId. This is because we defined the primary key on ourPondmodel as anobjectId.
Again, that the Pond schema doesn't change because the Pond has no relationship back to Frog.
{ "title": "Frog", "type": "object", "required": [ "_id", "name" ], "properties": { "_id": { "bsonType": "objectId" }, "age": { "bsonType": "long" }, "favoritePonds": { "bsonType": "array", "items": { "bsonType": "objectId" } }, "name": { "bsonType": "string" } } }
Inverse Relationship
Una relación inversa vincula un objeto con otros objetos que lo referencian en una relación definida uno-a-uno o uno-a-muchos denominada vínculo inverso. Para obtener más información sobre cómo se definen las relaciones inversas en Kotlin SDK, consulta Define una relación inversa.
App Services schemas do not support inverse relationships. This is because inverse relationship represent an implicit relationship in Realm that is automatically updated when the backlink is modified. This means that you cannot directly set the value of an inverse relationship, and the relationship does not exist in Atlas. Instead, Realm derives and updates those relationships for you in the client application based on your Realm object model.
Consider a case where the Pond object has an inverse relationship to the Frog object.
class Pond : RealmObject { var _id: ObjectId = ObjectId() var name: String = "" // Backlink to the `Frog` that has this `Pond` as its favorite val frog: RealmResults<Frog> by backlinks(Frog::favoritePonds) } class Frog : RealmObject { var _id: ObjectId = ObjectId() var name: String = "" var age: Int? = null // To-many relationship (can have many ponds) var favoritePonds: RealmList<Pond> = realmListOf() }
In the App Services schema, we see the Frog has the to-many relationship to the Pond through the favoritePonds property. However, the frog property that represents the inverse relationship to a Frog from our Pond model is not present. This is because the inverse relationship cannot be explicitly defined in Atlas. However, Realm will update it automatically whenever you add or remove an object from the relationship.
// `Pond` schema in App Services DOES NOT contain the // `frog` inverse relationship property { "title": "Pond", "type": "object", "required": [ "_id", "name" ], "properties": { "_id": { "bsonType": "objectId" }, "name": { "bsonType": "string" } } } { "title": "Frog", "type": "object", "required": [ "_id", "name" ], "properties": { "_id": { "bsonType": "objectId" }, "age": { "bsonType": "long" }, "favoritePonds": { "bsonType": "array", "items": { "bsonType": "objectId" } }, "name": { "bsonType": "string" } } }
objeto incrustado
Embedded objects represent nested data inside of a single, specific parent object. You can reference an embedded object type from parent object types in the same way as you would define a relationship. For more information on how embedded objects are defined in Kotlin SDK, refer to Define an Embedded Object.
Sin embargo, a diferencia de los objetos Realm, los objetos incrustados no se almacenan en su propia colección en Atlas. En su lugar, se almacenan como parte del documento del objeto principal y no se puede acceder a ellas fuera del objeto principal.
In the following example, we have a Frog object with a favoritePond property that references a single embedded Pond object and a Forest object with forestPonds relationship property that references a list of many embedded Pond objects:
class Frog : RealmObject { var _id: ObjectId = ObjectId() var name: String = "" var age: Int? = null // Embed a single object (MUST be optional) var favoritePond: EmbeddedPond? = null } class Forest : RealmObject { var _id: ObjectId = ObjectId() var name: String = "" // Embed multiple objects (can have many ponds) var forestPonds: RealmList<EmbeddedPond> = realmListOf() } class EmbeddedPond : EmbeddedRealmObject { var name: String? = null }
En el esquema de Servicios de aplicación, se observa que los objetos incrustados se mapean en documentos de cada tipo principal:
{ "title": "Frog", "type": "object", "required": [ "_id", "name" ], "properties": { "_id": { "bsonType": "objectId" }, "age": { "bsonType": "long" }, "favoritePond": { "title": "EmbeddedPond", "type": "object", "required": [], "properties": { "name": { "bsonType": "string" } } }, "name": { "bsonType": "string" } } } { "title": "Forest", "type": "object", "required": [ "_id", "name" ], "properties": { "_id": { "bsonType": "objectId" }, "forestPonds": { "bsonType": "array", "items": { "title": "EmbeddedPond", "type": "object", "required": [], "properties": { "name": { "bsonType": "string" } } } }, "name": { "bsonType": "string" } } }
Schema Types Mapping List
The following tables demonstrate how Realm object types map to a corresponding App Services schema BSON type. For a complete list of supported App Services schema types and their mappings and available properties, refer to Data Model Mapping in the App Services documentation.
Kotlin Types
La siguiente tabla enumera los tipos de dato de Kotlin admitidos y ejemplos de cómo las propiedades declaradas se asignan entre un esquema de Realm y un esquema de aplicación Services.
For more information on the Kotlin data types supported by the Kotlin SDK and how to define them in your data model, refer to Kotlin Data Types.
Tipo de datos de Kotlin | Realm Object | Esquema de aplicación Services | ||||
|---|---|---|---|---|---|---|
| | | ||||
| | | ||||
| | | ||||
| | | ||||
| | | ||||
| | | ||||
| | | ||||
| | | ||||
| | |
BSON Types
The following table lists the supported MongoDB BSON data types and examples of how the declared properties map between a Realm schema and an App Services schema.
For more information on the MongoDB BSON data types supported by the Kotlin SDK and how to define them in your data model, refer to Kotlin Data Types.
Tipo BSON de MongoDB | Realm Object | Esquema de aplicación Services | ||||
|---|---|---|---|---|---|---|
| | |||||
| | |
Realm Types
La siguiente tabla enumera los tipos de datos específicos de Realm compatibles y ejemplos de cómo las propiedades declaradas se asignan entre un esquema de Realm y un esquema de aplicación Services.
Para obtener información sobre los tipos de datos específicos de Realm que admite el SDK de Kotlin y cómo definirlos en su modelo de datos, consulte Tipos de datos de Kotlin.
Realm-Specific Type | Realm Object | Esquema de aplicación Services | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| | ||||||||||||
| | ||||||||||||
| | ||||||||||||
| | ||||||||||||
| | ||||||||||||
| | ||||||||||||
| | ||||||||||||
| | ||||||||||||
| |