realm-dotnet icon indicating copy to clipboard operation
realm-dotnet copied to clipboard

[Bug]: `EmbeddedObject.Parent` returning `null`

Open daawaan4U opened this issue 3 years ago • 1 comments

What happened?

EmbeddedObject.Parent is not returning anything after being added to realm

Repro steps

  1. Create a new RealmObject() with an EmbeddedObject, IList<EmbeddedObject, or IDictionary<TKey, EmbeddedObject> property
  2. Add RealmObject to Realm
  3. Populate the aforementioned property with a new EmbeddedObject()
  4. Accessing the Parent property of the added EmbeddedObject returns null

Version

10.7.1

What SDK flavour are you using?

Local Database only

What type of application is this?

Console/Server

Client OS and version

Windows 10 Home 20H2 19042.1348

Code snippets

Here's a small program for reproducing the bug

public class Test_EmbeddedObjects {

    class Item : RealmObject {
        public string? Label { get; set; }
        public ChildItem? ChildItem { get; set; }
        public IList<ChildItem> ChildItemList { get; } = null!;
        public IDictionary<string, ChildItem> ChildItemDict { get; } = null!;
    }

    class ChildItem : EmbeddedObject {
        public string? Label { get; set; }
    }

    public static void Main() {
        var config = new InMemoryConfiguration( "meemoo.db" ) {
            Schema = new Type[] { typeof( Item ), typeof( ChildItem ) }
        };
        var realm = Realm.GetInstance( config );

        var item = new Item { Label = "A" };
        item.ChildItem = new ChildItem { Label = "b" };
        realm.Write( () => {
            realm.Add( item );
            item.ChildItemList.Add( new ChildItem { Label = "c" } );
            item.ChildItemDict.Add( "d", new ChildItem { Label = "d" } );
        } );

        Console.WriteLine( item.ChildItem.Parent != null); // False
        Console.WriteLine( item.ChildItemList.First().Parent != null); // False
        Console.WriteLine( item.ChildItemDict.Values.First().Parent != null); // False
    }
}

Stacktrace of the exception/crash you're getting

No response

Relevant log output

No response

daawaan4U avatar Dec 06 '21 08:12 daawaan4U

Hi @daawaan, thanks for your clear report. Unfortunately that's an API that should have not been public as it is not yet implemented. We'll remove it in the next release until it's functioning properly.

For now you'll need to use backlinks to have a similar functionality:


    class Item : RealmObject
    {
        public string? Label { get; set; }
        public ChildItem? ChildItem { get; set; }
        public IList<ChildItem> ChildItemList { get; } = null!;
        public IDictionary<string, ChildItem> ChildItemDict { get; } = null!;
    }

    class ChildItem : EmbeddedObject
    {
        public string? Label { get; set; }

        [Backlink(nameof(Item.ChildItem))]
        public IQueryable<Item> ParentItems { get; }
    }

Unfortunately ParentItems will be set only for Item.ChildItem and not for the embedded objects in the list or dictionary though

EDIT: Relates to https://github.com/realm/realm-core/issues/3898

papafe avatar Dec 06 '21 10:12 papafe