I am building a Xamarin app for iOS and Android. The Realm on a login page is instantiated with the correct credentials from the ContentPage. I used Nito.AsyncEx to manage the context to get the realm, as follows…
AsyncContext.Run(async () =>
{
if (user == null || user.State == Realms.Sync.UserState.LoggedOut)
{
user = await app.LogInAsync(credentials);
}
var config = new PartitionSyncConfiguration(Constants.Partition, user);
config.ClientResetHandler =
new DiscardLocalResetHandler()
{
OnBeforeReset = HandleBeforeResetCallback,
OnAfterReset = HandleAfterResetCallback,
ManualResetFallback = HandleManualResetCallback
};
var realm = await Realm.GetInstanceAsync(config);
});
…which has been working fine thus far. But doing it this way doesn’t allow me to SubscribeForNotifications to any collections in the sync’d realm.
This works well for initially getting the realm, especially from a new installed version of the app, as it takes time to sync. However, using it looks like Nito.AsyncEx instantiates the Realm in a different Context, and any SubscribeForNotifications won’t receive any notifications, which I need in my app. I have tried lots of Task.Run, Task.Run.ContinueWith, and I’m having zero luck. Is there a way to instantiate the Realm within a NitoAsyncEx Context, then later subscribe for notifications to the Realm in a way that I do receive them? Or is there a way to instantiate the Realm without Nito.AsyncEx, but the LoginAsync, PartitionSyncConfiguration , GetInstanceAsync can be done (via a synchronous wrapper, perhaps?) in the proper order and the results from the two async methods (LoginAsync, GetInstanceAsync) are returned without a thread deadlock on the UI Context?