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 Data

Agregar sincronización de dispositivos a una aplicación - .NET SDK

1

Pase el ID de la aplicación para su aplicación, que puede encuéntrelo en la interfaz de usuario de servicios de aplicaciones.

app = App.Create(myRealmAppId);
2

Autentique un usuario en su proyecto de cliente. Aquí, usaremos autenticación anónima.

var user = await app.LogInAsync(Credentials.Anonymous());
3

To open the realm as a synced realm, you can specify whether a synced realm should download data before it opens. Here, we use a Flexible Sync configuration and specify that the SDK should always download the most recent updates before opening the realm. We also bootstrap the realm with an initial subscription.

var config = new FlexibleSyncConfiguration(app.CurrentUser)
{
PopulateInitialSubscriptions = (realm) =>
{
var myItems = realm.All<Item>().Where(n => n.OwnerId == myUserId);
realm.Subscriptions.Add(myItems);
}
};
// The process will complete when all the user's items have been downloaded.
var realm = await Realm.GetInstanceAsync(config);

La sintaxis para leer, escribir y observar cambios en un dominio sincronizado es idéntica a la de los dominios no sincronizados. Mientras trabaja con datos locales, un subproceso en segundo plano integra, carga y descarga conjuntos de cambios de forma eficiente.

El siguiente código demuestra dos formas de crear un nuevo Task objeto y agregarlo al reino:

var testItem = new Item
{
Name = "Do this thing",
Status = ItemStatus.Open.ToString(),
Assignee = "Aimee"
};
await realm.WriteAsync(() =>
{
realm.Add(testItem);
});
// Or
var testItem2 =
await realm.WriteAsync(() =>
{
return realm.Add<Item>(new Item
{
Name = "Do this thing, too",
Status = ItemStatus.InProgress.ToString(),
Assignee = "Satya"
});
}
);

Importante

Cuando utilice Sync, evite escrituras síncronas en el hilo principal

Because Realm performs sync integrations on a background thread, if you do a synchronous write to your realm on the main thread, there's a small chance your UI could appear to hang as it waits for the background sync thread to finish a write transaction. Therefore, it's a best practice not to do a synchronous write on the main thread when using Device Sync and instead use asynchronous writes with realm.WriteAsync.

Volver

Sync Data

En esta página