Xamarin forms List Refresh when editing values but not when adding or removing Items


#1

Thanks to @nirinchev for clarifying that I need to use IList instated of ObservableCollection in order to get and show the data from Realm Cloud, I am able to show the list and when I edit anything on the phone or the Realm Studio, the change is Reflected Directly on the list without me doing anything.

But, when I try to add or delete an item, the item will be added or removed from the back , but the list will not refresh automatically.

This is my code:

List Initialisation:

 private IList<DB_Realm.CrisisDB> _crisisList;

 public IList<DB_Realm.CrisisDB> CrisisList
 {

        get { return _crisisList; }
        protected set
        {
            Set(ref _crisisList, value);
        }
  }

Getting the Realm Items and Asigning them to the list:

 IList<DB_Realm.CrisisDB> parent = null;
           
 parent = _realm.All<DB_Realm.CrisisDB>().ToList();

 CrisisList = parent;

I used ViewModelBase and almost every other thing exactly as the Realm Tasks Sample App


#2

When you use ToList() you are calling the LINQ method to copy data in an empty regular list - it doesn’t have any notification abilities built in. Instead, you should use the Realm collection as is:

private IQueryable<DB_Realm.CrisisDB> _crisisList;

public IQueryable<DB_Realm.CrisisDB> CrisisList
{
    get { return _crisisList; }
    protected set { Set(ref _crisisList, value); }
}

// And then...
IQueryable <DB_Realm.CrisisDB> parent = null;
           
parent = _realm.All<DB_Realm.CrisisDB>();

CrisisList = parent;

You can read more about it in the docs.


#3

Thanks for the quick reply. I will try it right now.


#4

It worked. Many thanks.