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.
Cuando el modo de desarrollo está habilitado, los campos que se incluyen en las consultas de suscripción de cliente se agregan automáticamente como campos consultables en Atlas. Para obtener más información sobre la configuración de suscripciones con el SDK de Kotlin, consulte la sección "Descripción general de suscripciones".
Mapeo de Objetos Realm
Los objetos de Realm son instancias con nombres únicos de clases Kotlin definidas en su esquema de Realm que determinan las propiedades y relaciones de los objetos de ese tipo.
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.
Usando los objetos del ejemplo anterior, considere un caso donde un Frog puede tener un estanque favorito. Podemos agregar una propiedad favoritePond a nuestro modelo Frog que sea un enlace opcional a un objeto Pond.
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:
El campo no está en la matriz
requiredporque es una propiedad opcional.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.
El esquema Pond no cambia. Al ser una relación de uno a uno, es unidireccional; Pond no tiene relación con Frog.
{ "title": "Frog", "type": "object", "required": [ "_id", "name" ], "properties": { "_id": { "bsonType": "objectId" }, "age": { "bsonType": "long" }, "favoritePond": { "bsonType": "objectId" }, "name": { "bsonType": "string" } } }
Relación de muchos
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:
El campo no está en la matriz
requiredporque es una propiedad opcional.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" } } }
Relación inversa
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
Las siguientes tablas muestran cómo los tipos de objeto de Realm se asignan a un tipo BSON de esquema de App Services correspondiente. Para obtener una lista completa de los tipos de esquema de App Services compatibles, sus asignaciones y propiedades disponibles, consulte "Asignación de modelos de datos" en la documentación de App Services.
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.
Para obtener más información sobre los tipos de datos de Kotlin compatibles con el SDK de Kotlin y cómo definirlos en su modelo de datos, consulte Tipos de datos de Kotlin.
Tipo de datos de Kotlin | Realm Object | Esquema de aplicación Services | ||||
|---|---|---|---|---|---|---|
| | | ||||
| | | ||||
| | | ||||
| | | ||||
| | | ||||
| | | ||||
| | | ||||
| | | ||||
| | |
BSON Types
En la siguiente tabla se enumeran los tipos de datos BSON de MongoDB admitidos y ejemplos de cómo las propiedades declaradas se asignan entre un esquema de Realm y un esquema de App Services.
Para obtener más información sobre los tipos de datos BSON de MongoDB compatibles con el SDK de Kotlin y cómo definirlos en su modelo de datos, consulte Tipos de datos de Kotlin.
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 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| | ||||||||||||
| | ||||||||||||
| | ||||||||||||
| | ||||||||||||
| | ||||||||||||
| | ||||||||||||
| | ||||||||||||
| | ||||||||||||
| |