realm-dotnet
realm-dotnet copied to clipboard
[Bug]: `EmbeddedObject.Parent` returning `null`
What happened?
EmbeddedObject.Parent
is not returning anything after being added to realm
Repro steps
- Create a
new RealmObject()
with anEmbeddedObject
,IList<EmbeddedObject
, orIDictionary<TKey, EmbeddedObject>
property - Add
RealmObject
to Realm - Populate the aforementioned property with a
new EmbeddedObject()
- Accessing the
Parent
property of the addedEmbeddedObject
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
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