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.
Asymmetric Objects
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
AsymmetricRealmObjectpuede contenerEmbeddedRealmObjecttipos, pero no puede contenerRealmObjecttipos ni otros tipos deAsymmetricRealmObject.AsymmetricRealmObjecttypes 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.
Sync Data Unidirectionally from a Client Application
Definir un objeto asimétrico
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 { var id: ObjectId = ObjectId() var deviceId: String = "" var temperatureInFarenheit: Float = 0.0F var barometricPressureInHg: Float = 0.0F var windSpeedInMph: Int = 0 }
Connect and Authenticate with an App Services App
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.
Abrir un reino
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.
Create Asymmetric Objects
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.