-
Hi! I'm making an application with MAUI. I have two ProfileInfo (simplified)public partial class ProfileInfo : RealmObject
{
[PrimaryKey]
public Guid ID { get; set; }
public ServiceInfo Service { get; set; }
public string Username { get; set; }
public string Password { get; set; }
} ServiceInfo (simplified)public partial class ServiceInfo : RealmObject
{
[PrimaryKey]
public Guid ID { get; set; }
/// <summary>
/// Service name
/// </summary>
[Indexed]
public string Name { get; set; } = string.Empty;
/// <summary>
/// List of <see cref="ProfileInfo"/>s linked to <see cref="ServiceInfo"/>
/// </summary>
[Backlink(nameof(ProfileInfo.Service))]
public IQueryable<ProfileInfo> Profiles { get; }
} When I delete service with profiles linked to it then add new profile (with any other service) application instantly crashes. It does the same behavior in testing: [Test]
public void RemoveServiceWithProfilesTest()
{
RunTestWithDatabaseAsync(async (databaseService) =>
{
SettingsViewModel viewModel = new SettingsViewModel(databaseService, alertService);
RelayCommand<ServiceInfo> command = (RelayCommand<ServiceInfo>)viewModel.RemoveServiceCommand;
ServiceInfo service = new ServiceInfo()
{
Name = service_name,
};
databaseService.Add(service);
ProfileInfo[] profileInfos = ProfileData.GetTestProfiles(service);
foreach (ProfileInfo profile in profileInfos)
databaseService.Add(profile);
Assert.DoesNotThrow(() => command.Execute(service));
databaseService.Refresh().Wait();
Assert.That(databaseService.Select<ServiceInfo>().Any(s => s.Name == service_name), Is.False);
foreach (ProfileInfo profile in profileInfos)
Assert.That(databaseService.Select<ProfileInfo>().Any(p => p.ID == profile.ID), Is.False);
});
} I've tried |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Can you share the stack trace of the exception? That should point us to where the object is being accessed. Alternatively, if you can create an isolated project that reproduces the problem, that would be super helpful. The code snippets you posted are useful, but there's a lot of details that are not available, making it hard to determine what the root cause is. |
Beta Was this translation helpful? Give feedback.
-
I've moved logic to an isolated project and got the same exception.
using Realms;
using System.Diagnostics;
Realm realm = Realm.GetInstance(new InMemoryConfiguration(new Guid().ToString()));
string service_name = "some service";
ServiceInfo service = new (service_name);
List<ProfileInfo> profiles = new List<ProfileInfo>
{
new ProfileInfo(Guid.NewGuid(), service, "user1", "password1"),
new ProfileInfo(Guid.NewGuid(), service, "user2", "password2"),
};
realm.Write(() =>
{
realm.Add(service);
});
//Simulating real scenario, profiles and services are in different transactions
realm.Write(() =>
{
foreach (var profile in profiles)
realm.Add(profile);
});
await realm.RefreshAsync();
await realm.WriteAsync(() =>
{
realm.RemoveRange(service.Profiles);
realm.Remove(service);
});
await realm.RefreshAsync();
Debug.Assert(!realm.All<ServiceInfo>().Any(s => s.Name == service_name));
foreach (ProfileInfo profile in profiles)
Debug.Assert(!realm.All<ProfileInfo>().Any(p => p.ID == profile.ID));
public partial class ProfileInfo : RealmObject
{
[PrimaryKey]
public Guid ID { get; set; }
public ServiceInfo Service { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public ProfileInfo()
{
ID = Guid.NewGuid();
}
public ProfileInfo(Guid id, ServiceInfo service, string username, string password)
{
ID = id;
Service = service;
Username = username;
Password = password;
}
}
public partial class ServiceInfo : RealmObject
{
[PrimaryKey]
public Guid ID { get; set; }
/// <summary>
/// Service name
/// </summary>
[Indexed]
public string Name { get; set; } = string.Empty;
/// <summary>
/// List of <see cref="ProfileInfo"/>s linked to <see cref="ServiceInfo"/>
/// </summary>
[Backlink(nameof(ProfileInfo.Service))]
public IQueryable<ProfileInfo> Profiles { get; }
public ServiceInfo(string name) : base()
{
Name = name ?? string.Empty;
}
public ServiceInfo()
{
ID = Guid.NewGuid();
}
} One thing I'm uncertain aboutIn my app I got the same exception except that it was thrown from a different class. Stack traces of exceptions from both projects (both .net 7.0) Isolated project
My app
|
Beta Was this translation helpful? Give feedback.
Hey sorry for the delay - I was able to reproduce the issue with the code you posted and it seems like things are working by design. What's happening is that you're adding a couple of
Profile
instances, associated with a service. Then you're removing them through theservice.Profiles
backlink and removing the service. Then you're trying to execute the following queryrealm.All<ProfileInfo>().Any(p => p.ID == profile.ID)
.profile
here is a deleted object, so when you try to readprofile.ID
, you're getting the exception.If you want to avoid the error, you probably need to extract the ids prior to deleting the objects - for example, doing something like: