'Adding ModifiedAt throws RealmInvalidTransactionException
I am writing a simple WPF app that uses Realm Database.
Each time a database record is changed, I want to update its ModifiedAt property, but somehow it keeps throwing this error:
Realms.Exceptions.RealmInvalidTransactionException: 'Cannot modify managed objects outside of a write transaction.'
Here is my code where I subscribe to listen to changes and update that property. The exception is thrown inside foreach loop.
public DreamRepository(Realm realmInstance) : base(realmInstance)
{
_realmInstance = realmInstance;
_realmInstance.All<DreamEntity>().SubscribeForNotifications((sender, changes, error) =>
{
foreach (var dream in sender)
{
dream.ModifiedAt = DateTime.Now;
}
});
}
I tried to debug it, this code is triggered after most of operations get completed because of asynchrony, so I am guessing maybe the instance gets disposed by the time when it's triggered, but I don't know how to really check that.
It works pretty well in Console App but not in a WPF app.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
