Hi, I have wrote a simple code like old Realm Legacy.
I write a .Net Web App that connect to Sync MongoDbRealm. Collection appear on server, and when I create a new object from client, I view it on server.
But if I update or delete any document from server nothing appened on client
Permission is true/true for read & write
With MongoDbRealm, when I run NewCliente(…), a new Record is added in MongoCollection and it’s visible from view Index.
If I add record on MondoDB collection, Index() not show new record
namespace StudioProTest.Models
{
public class Cliente : RealmObject
{
[PrimaryKey]
[MapTo("_id")]
public string ID { get; set; } = Guid.NewGuid().ToString();
[MapTo("_partition")]
public string _partition { get; set; }
[MapTo("nome")]
public string Nome { get; set; }
[MapTo("cognome")]
public string Cognome { get; set; }
[MapTo("isDone")]
public bool IsDeleted { get; set; }
[MapTo("timestamp")]
public DateTimeOffset Timestamp { get; set; } = DateTime.Now;
}
}
Controller.cs
async public static Task GetRealm()
{
App app = App.Create(new AppConfiguration(Settings.AppId)
{
MetadataPersistenceMode = MetadataPersistenceMode.NotEncrypted,
LocalAppName = "TestNewRealm",
LocalAppVersion = "1.0.0"
});
User user = await app.LogInAsync(Credentials.Anonymous());
var config = new SyncConfiguration("test", user);
Realm realm = await Realm.GetInstanceAsync(config);
return realm;
}
public async Task NewCliente(string nome, string cognome)
{
var vRealmDb = await GetRealm();
Transaction trans = vRealmDb.BeginWrite();
Cliente cliente = new Cliente
{
Nome = nome,
Cognome = cognome
};
vRealmDb.Add(cliente);
trans.Commit();
return Redirect(nameOf(Index));
}
public async Task Index()
{
var vRealmDb = await GetRealm();
var list = vRealmDb.All<Cliente>();
return View(list);
When working with Realm instances on background threads (such as in console app or a web server), you’ll need to manually call Refresh to force the Realm to update itself. Opening a Realm instance for every request should be fine and it should automatically get refreshed, but you’re hitting a combination of issues:
You’re not disposing of Realm files. This is generally a bad idea and means that the native instances will outlive your requests. This means that you can randomly get a reference to a cached native instance if a request happens to execute on the same thread as a previous one.
GetInstance would have refreshed the Realm, but GetInstanceAsynchas a bug and doesn’t refresh the Realm prior to resolving the task.
The combination of these means that you may end up with a stale Realm that needs to be manually refreshed. While we will fix the bug described in 2., I would strongly recommend that you dispose your Realm instances when you’re no longer using them. This will free native resources predictably and, more importantly, will prevent explosive file size growth.
Regarding Xamarin - this should not happen there because Xamarin apps have a main thread which allows Realm to automatically refresh itself in the background. If you open Realm instances on background threads in Xamarin apps, the recommendation is, again, to dispose of them as soon as you’re done using them.