Child Collection and Backlink


#1

I have Item and Inventory class defined as below:

public class ItemEntity: RealmObject{
        public IList<ItemInventoryEntity> Inventories { get; }

        public ItemEntity() : base()
        {
            Inventories.AsRealmCollection().CollectionChanged += Inventories_CollectionChanged;
        }
        private void Inventories_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
          ...
        }
}
  1. I want to get notified by
    Inventories.AsRealmCollection().CollectionChanged
    but this throws
    list must be an instance of IRealmCollection<ItemInventoryEntity>
    how can I catch the child collection change event from parent?

  2. How to define Backlink in the ItemInventory class? I tried following but it doesn’t compile

        [Backlink(nameof(ItemEntity.Inventories))]
        public ItemEntity Item { get; }

Error Fody/RealmWeaver: ItemInventoryEntity.Item has [Backlink] applied, but is not IQueryable.


#2

For the second error - it seems like you’re applying the attribute on the wrong property. If your model is such that one ItemInventoryEntity points to one ItemEntity, and one ItemEntity may have many ItemInventoryEntity, then your models should look like:

public class ItemEntity : RealmObject
{
    [Backlink(nameof(ItemInventoryEntity.Item))]
    public IQueryable<ItemInventoryEntity> Inventories { get; }
}

public class ItemInventoryEntity : RealmObject
{
    public ItemEntity Item { get; set; }
}