Search code examples
xamarin.formsmvvmfody-propertychanged

Xamarin Forms: Propertychanged is not triggered when refreshing from database


I'm having an object in my viewmodel which consists of an Id and some other attributes. my equality check contains the check on Id, so I can identify items in lists when refreshing.

public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != GetType()) return false;
            return Equals(obj as Class);
        }
public bool Equals(Class other)
        {
            if (other == null)
            {
                return false;
            }

            return this.Id.Equals(other.Id);
        }

Now when I'm on a detailPage which is only having one item, I want to manually refresh that item from the database. When doing my refresh the Id obviously does not change, but the remaining attributes can. Now the problem I have is that the Propertychanged is not triggered when item is identical(based on id).

Class refreshedItem = await Task.Run(() => backendConnection.GetSchedule(scheduleId, DTOType.Maxi, BackEndSettings.Default.SystemOfMeasurement));
Item = refreshedItem;

If I set the Item to a dummy item in between it is triggered every time.

Class refreshedItem = await Task.Run(() => backendConnection.GetSchedule(scheduleId, DTOType.Maxi, BackEndSettings.Default.SystemOfMeasurement));
Item = new Class(Guid.Empty) { Title = "test" };
Item = refreshedItem;

Solution

  • Looking at Github Fody Properties Overview, the (default) code that is injected in Item setter is equivalent to

    if (value != item)
        OnPropertyChanged();
    

    It only fires when not equal.

    The documentation for Fody Properties says to use Attributes to control its behavior. For what you want, use DoNotCheckEqualityAttribute.