Is this normal behavior? PropertyChanged

public IRealmCollection<FuCode> FuCodes { get; private set; }
private Realm _realm;

public async Task SubscribeAsync(CancellationToken cancellationToken)
        {
            _realm = await Realm.GetInstanceAsync(cancellationToken: cancellationToken);
            FuCodes = _realm.All<FuCode>().AsRealmCollection();
            FuCodes.CollectionChanged += OnSubscribeCollectionChanged;

            SubscribeForPropertyChanged(FuCodes);  //NO1
        }

        private void OnSubscribeCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                SubscribeForPropertyChanged(e.NewItems.Cast<FuCode>());  //NO2
            }
        }

        private void SubscribeForPropertyChanged(IEnumerable<FuCode> fuCodes)
        {
            foreach (FuCode fuCode in fuCodes)
            {
                fuCode.PropertyChanged += OnSubscribePropertyChanged;
            }
        }

        private void OnSubscribePropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            L.Trace($"{e.PropertyName}");
        }

NO1 → not fire propertychagned!!!
NO2 → fire propertychagned!!!

why?

Hi @lasidolasido, thanks for your message.

I think it’s a little difficult to understand what’s happening here without looking at exactly what you are doing and how you are modifying your collection. Nevertheless, what I suppose is happening is:

  • NO1, you are subscribing to PropertyChanged only for the FuCodes that were already in the collection when you opened the realm (that could be even empty)
  • NO2, you are subscribing to PropertyChanged only for the newly added FuCodes

I suppose you’re adding new FuCodes to the list and then changing their properties, and that’s why you get only notifications in the second case.

2 Likes