1
votes

I am working on Xamarin forms project targeting iOS/Droid/UWP. We are building the app in a reactive manner and use direct model bindings. For instance, let's imagine we have the following code

public class Product : RealmObject
    {
        public string Name { get; set; }
    }
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        public static string LocalPath = "";
        public MainPage()
        {
            InitializeComponent();
            var realm = Realm.GetInstance(Path.Combine(LocalPath, "RealmSample.realm"));
            realm.Write(() =>
            {
                realm.Add(new Product()
                {
                    Name = "Test"
                });
            });
            BindingContext = realm.All<Product>().FirstOrDefault();
        }
    }

And the following xaml file

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="RealmBindingSample.MainPage">

    <StackLayout>
        <Entry 
            Text="{Binding Name}"/>
    </StackLayout>

</ContentPage>

Nothing is special in here. And this code works just fine on iOS/Droid and the data is persisted as soon as value of the Entry changes. However, on UWP as soon as the page is loaded I get the following exception

Cannot modify managed objects outside of a write transaction.

Is this expected behavior in UWP? Am I missing something.

I have installed the Realm.Database nuget version 3.4.0, and the FodyWeavers.xml files are present in all of the projects.

1
I created a code sample with RealM version 4.0 nuget , and it works well. This is test sample. - Nico Zhu - MSFT
@NicoZhu-MSFT thanks, it was exactly that. - kyurkchyan

1 Answers

2
votes

I've got a reply to this issue in the official github issues of the Realm.Net library.

Two way data binding doesn't work with UWP with Realm 3.4.0 because it's a netstandard 1.4 library and the classes needed to implement automatic transactions were only added in netstandard 2.0. You can upgrade Realm to 4.0.0, but be advised that that will require upgrading your UWP project to target netstandard 2.0.

The problem was in the version of the Realm nuget package I was using. I used Realm.Database nuget latest 3.4.0 version. As soon as I replaced the package with Realm nuget version 4.0.0 everything worked just fine.