Connect to the App Services backend
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);
Autenticar a un usuario
Autentique un usuario en su proyecto de cliente. Aquí, usaremos autenticación anónima.
var user = await app.LogInAsync(Credentials.Anonymous());
Open a Synced Realm
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);
Use the Realm
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.