Overview
When writing data to a synced realm using Flexible Sync, you use the same APIs you use when writing to a local realm. However, there are some differences in behavior to keep in mind.
When you write to a synced realm, your write operations must match both of the following:
La query de suscripción de sincronización
The permissions in your App Services App
Si intenta escribir datos que no coinciden con la consulta de suscripción y la expresión de permisos, la escritura se revierte y genera un error. CompensatingWriteException. Puede consultar los registros de App Services para ver detalles sobre por qué se denegó la escritura.
Compensating Writes
When a client attempts an "illegal" write of an object, the following occurs:
Como el realm del cliente no tiene concepto de guardados "ilegales", el guardado tiene éxito inicialmente hasta que realm resuelve el conjunto de cambios con el backend de Servicios de aplicación.
Upon sync, the server applies the rules and permissions. The server determines that the user does not have authorization to perform the write.
The server sends a revert operation, called a "compensating write", back to the client.
El reino del cliente revierte la operación de escritura ilegal y lanza un
CompensatingWriteException.
Any client-side writes to an object between the illegal write and the corresponding compensating write will be lost.
En la práctica, esto puede parecer que se escribe un objeto en el realm y luego desaparece después de que el servidor envía el guardado compensatorio de vuelta al cliente.
Cuando se lanza un CompensatingWriteException, incluye una colección enumerable de objetos CompensatingWriteInfo. Cada objeto CompensatingWriteInfo contiene propiedades que describen el tipo de objeto Realm, su llave primaria y razón por la que el servidor realizó la guardar compensatoria.
Determining Which Data Syncs
The rule that controls which data you can write to a synced realm is the intersection of your Device Sync configuration, App Services permissions, and the Flexible Sync subscription query that you use when you open the realm.
Veamos un ejemplo de cómo esos componentes funcionan juntos:
Configuración de sincronización del dispositivo
Sync de dispositivo se configura con los siguientes campos consultables:
_id(siempre incluido)ownerId
Permisos de servicios de aplicaciones
The App Services App has permissions configured to let users read and write only their own data:
{ "name": "owner-read-write", "apply_when": {}, "document_filters": { "read": { "ownerId": "%%user.id" }, "write": { "ownerId": "%%user.id" } }, "read": true, "write": true }
Suscripción Sync Flexible
The flexible sync subscription on the client states that the object must have a Status value of "completed":
realm.Subscriptions.Update(() => { var completedItemsQuery = realm .All<MyTask>() .Where(i => i.Status == "completed"); realm.Subscriptions .Add(completedItemsQuery, new SubscriptionOptions() { Name = "completedItems" }); });
The Result: Which Data Syncs?
The combination of the subscription query and the permissions means that the synced realm only syncs objects where:
The
ownerIdmatches theuser.idof the logged-in user (from the permissions)The
Statusproperty's value is "completed" (from the subscription query)
Any object in the Atlas collection where the ownerId does not match the user.id of the logged-in user, or the Status property's value not "completed", cannot sync to this realm. An attempt to write such an object throws a CompensatingWriteException.
For More Information
Para obtener más información sobre los errores de denegación de permisos, errores de escritura compensatoria y otros tipos de errores de Device Sync, consulta Errores de sincronización en la documentación de App Services.
To learn more about configuring permissions for your app, see Role-based Permissions and the Device Sync Permissions Guide in the App Services documentation.