The error “Sync session became inactive” is occurring numerous times in our mobile app using the following code;
private async void OnCompleted(object sender, EventArgs e)
{
bool cancel = false;
bool finishService = false;
Realm realm = null;
(bool IsSynced, Realm Realm) result = (false, realm);
try
{
ServiceResponse svcResponse = new ServiceResponse
{
...
};
result = await ConnectionService.GetSyncedRealm(SxRealm.Shared.Enums.AppConfiguration.DriverApp, _appUser.RealmId, _user);
if (result.Realm == null)
{
throw new Exception(Properties.Resources.ApplicationSyncConnectionFail);
}
realm = result.Realm;
_isSynced = result.IsSynced;
ServiceResponseService serviceResponseSvc = new ServiceResponseService();
svcResponse = serviceResponseSvc.AddOrUpdate(realm, svcResponse);
_responseId = svcResponse.ID;
svcResponse.Adapt(_service, MapsterConfigs.ServiceResponseToServiceVmMap);
finishService = true;
}
catch (Exception ex)
{
AppConfig.LogException(ex, _appUser.TestMode, logEntries);
await DisplayAlert("Save Service", ex.GetFirstMessage(), Properties.Resources.ApplicationOk);
}
finally
{
realm?.Dispose();
if (finishService) FinishService(cancel);
}
}
public static async Task<(bool IsSynced, Realm Realm)> GetSyncedRealm(Enums.AppConfiguration appConfig, string partition, User realmUser, int timeout = 15)
{
bool isSynced;
Realm realm;
PartitionSyncConfiguration config = GetSyncConfig(appConfig, partition, realmUser);
(bool IsSynced, Realm Realm) result = await GetSyncedRealm(config, timeout);
realm = result.Realm;
isSynced = result.IsSynced;
if (result.Realm != null && result.IsSynced == false)
{
await realm.SyncSession.WaitForDownloadAsync();
isSynced = true;
}
if (realm == null)
{
realm = GetLocalRealm(config);
if (realm == null) throw new Exception("Failed to open a synchronised or local instance of the Realm data.");
}
return (isSynced, realm);
}
private static async Task<(bool Synchronised, Realm Realm)> GetSyncedRealm(PartitionSyncConfiguration appConfig, int timeout = 15)
{
Realm realm = null;
CancellationTokenSource cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromSeconds(timeout));
try
{
realm = await Realm.GetInstanceAsync(appConfig, cts.Token);
return (true, realm);
}
catch (OperationCanceledException)
{
return (false, realm);
}
catch (Exception)
{
throw;
}
}
The stack trace returned is;
at Realms.Sync.SyncConfigurationBase.CreateHandleAsync (Realms.Schema.RealmSchema schema, System.Threading.CancellationToken cancellationToken) [0x00117] in :0
at Realms.RealmConfigurationBase.CreateRealmAsync (System.Threading.CancellationToken cancellationToken) [0x0007d] in :0
at SxRealm.ConnectionService.GetSyncedRealm (Realms.Sync.PartitionSyncConfiguration appConfig, System.Int32 timeout) [0x000be] in <893c83867d6245f88960faaae99610a2>:0
at SxRealm.ConnectionService.GetSyncedRealm (SxRealm.Shared.Enums+AppConfiguration appConfig, System.String partition, Realms.Sync.User realmUser, System.Int32 timeout) [0x00090] in <893c83867d6245f88960faaae99610a2>:0
at SxDriverApp.Views.ServicePage.OnCompleted (System.Object sender, System.EventArgs e) [0x00342] in <4dda95a5100c49869b508a8a0fe02bb2>:0
I can’t find any reference to the error message but I’m not sure why the session would become inactive immediately after opening the connection.