Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /
Sync Device Data

Stream Data to Atlas - Kotlin SDK

Novedades en la versión 1.10.0.

Si tienes una aplicación móvil o de cliente que genera un gran volumen de datos que deseas transmitir a MongoDB Atlas, puedes sincronizar datos unidireccionalmente utilizando Device Sync. Llamamos a la funcionalidad que permite esta sincronización unidireccional Data Ingest.

Puede usar Ingesta de datos para transmitir datos desde la aplicación cliente a una aplicación Atlas App Services habilitada para Flexible Sync.

You might want to sync data unidirectionally in IoT applications, such as a weather sensor sending data to the cloud. Data Ingest is also useful for writing other types of immutable data where you do not require conflict resolution, such as creating invoices from a retail app or logging application events.

Data Ingest is optimized to provide performance improvements for heavy client-side insert-only workloads.

Data Ingest requiere Flexible Sync.

El SDK de Kotlin proporciona un tipo especial de objeto Realm para usar con la ingesta de datos: un objeto asimétrico.

Los objetos asimétricos admiten ampliamente los mismos tipos de propiedades que RealmObject, con algunas excepciones:

  • Asymmetric objects can only be used in synced realms configured with Flexible Sync. However, you cannot create subscriptions to asymmetric objects.

  • Un AsymmetricRealmObject puede contener EmbeddedRealmObject tipos, pero no puede contener RealmObject tipos ni otros tipos de AsymmetricRealmObject.

  • AsymmetricRealmObject types cannot be used as properties in other Realm objects.

Additionally, asymmetric objects do not function in the same way as other Realm objects. You cannot add, read, update, or delete an asymmetric object from the realm. You can only create an asymmetric object, which then syncs unidirectionally to the Atlas database linked to your App with Device Sync. Realm then deletes this object after syncing.

Tip

Objetos asimétricos y no asimétricos en el mismo Realm

The Kotlin SDK allows you to work with asymmetric objects and non-asymmetric Realm objects (RealmObject and EmbeddedRealmObject types) within the same realm. Include all objects in the Sync configuration when you open the realm. However, you cannot subscribe to the asymmetric objects.

1

Puedes sincronizar datos unidireccionalmente cuando ese objeto es un AsymmetricRealmObject. Define un objeto asimétrico implementando la interfaz AsymmetricRealmObject:

// Implements the `AsymmetricRealmObject` interface
class WeatherSensor : AsymmetricRealmObject {
@PersistedName("_id")
@PrimaryKey
var id: ObjectId = ObjectId()
var deviceId: String = ""
var temperatureInFarenheit: Float = 0.0F
var barometricPressureInHg: Float = 0.0F
var windSpeedInMph: Int = 0
}
2

Para transmitir datos desde el cliente a su aplicación backend, debe conectarse a un backend de App Services y autenticar a un usuario.

val app = App.create(YOUR_APP_ID)
val user = app.login(credentials)

Data Ingest es una funcionalidad de Flexible Sync, por lo que la aplicación a la que te conectes debe usar Flexible Sync.

3

After you have an authenticated user, you can open a synced realm using a SyncConfiguration. Specify the AsymmetricRealmObject types you want to sync.

val config = SyncConfiguration.create(user, setOf(WeatherSensor::class))
val realm = Realm.open(config)
Log.v("Successfully opened realm: ${realm.configuration.name}")

A diferencia de la sincronización bidireccional, la Data Ingest no utiliza una suscripción de Flexible Sync. Si tienes objetos no asimétricos en el mismo ámbito, puedes agregar una consulta de suscripción de Sincronización flexible solo para esos objetos.

4

Una vez que tengas un Realm abierto, puedes crear un AsymmetricRealmObject durante una transacción de escritura usando el método de extensión insert():

// Open a write transaction
realm.write {
// Create a new asymmetric object
val weatherSensor = WeatherSensor().apply {
deviceId = "WX1278UIT"
temperatureInFarenheit = 6.7F
barometricPressureInHg = 29.65F
windSpeedInMph = 2
}
// Insert the object into the realm with the insert() extension method
insert(weatherSensor)
// WeatherSensor object is inserted into the realm, then synced to the
// App Services backend. You CANNOT access the object locally because it's
// deleted from the local realm after sync is complete.
}

You cannot read these objects. Once created, they sync to the App Services backend and the linked Atlas database.

Atlas Device Sync gestiona completamente el ciclo de vida de estos datos. Se mantiene en el dispositivo hasta que la sincronización de Data Ingest se completa y luego se elimina del dispositivo.

Volver

Set the Client Log Level

En esta página