
public class User : RealmObject { public string Id { get; set; } = Guid.NewGuid().ToString(); public string Name { get; set; } /*    – ,   . . */ } User class, there is no need to “update” it, because whenever you access the property, the current information is saved. In addition, RealmObject implements INotifyPropertyChanged , which allows you to subscribe and receive notifications about any possible changes. And although these are just a few lines of code, their impact is very significant. And even better, there are actually no templates here - there is no manual triggering of the event, SQL mapping, and, of course, the update logic.ObservableCollection<User> in a ViewModel , linking it, and then updating when models are changed (for example, after adding a new contact). It sounds like it is very difficult, but look at how we will cope with this task with the help of Realm: public class ContactsViewModel { private readonly Realm _realm; public IEnumerable<User> Users { get; } public ContactsViewModel() { _realm = Realm.GetInstance(); Users = _realm.All<User>().OrderBy(u => u.Name); } } ContactsViewModel in the code as a BindingContext , but there’s nothing interesting there, so let's just assume that we did it): <ContentPage x:Class="Contacts.ContactsPage"> <ContentPage.Content> <ListView ItemsSource="{Binding Users}" x:Name="ContactsListView"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <Label Text="{Binding Name}"/> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> </ContentPage.Content> </ContentPage> Users no longer required. The runtime type of the collection returned by Realm.All implements INotifyCollectionChanged , which is monitored by the Xamarin Forms data binding mechanism, so that the interface is notified of any changes that occur in the collection. When working with a native UI project, you can either convert this type yourself, or use the AsRealmCollection extension method . Now, in confirmation of my words, let's consider how we can make certain changes. public class EditContactViewModel { public User User { get; } public EditContactViewModel(User user) { User = user; } }  <ContentPage x:Class="Contacts.EditContactPage" Title="{Binding User.Name}"> <ContentPage.Content> <Entry Text="{Binding User.Name}" Placeholder="Contact name" /> </ContentPage.Content> </ContentPage> Entry bindings become bidirectional by default, whenever you change a username, the change is saved to disk and the page title is updated. And since our ListView on the main screen is associated with an “active” query, it will update the relevant new data already by pressing the “back” button. Connecting navigation to view models is not so fun, so I will not dwell on this here. But with one of the ways to do this can be found in the finished example .
 public class User : RealmObject { /* previous properties */ public bool IsFavorite { get; set; } } ContactsViewModel so that the selected contacts are shown above, and also add the command “Switch favorites”: public class ContactsViewModel { /* Other properties */ public Command<User> ToggleIsFavoriteCommand { get; } public ContactsViewModel() { _realm = Realm.GetInstance(); Users = _realm.All<User>().OrderByDescending(u => u.IsFavorite) .ThenBy(u => u.Name); ToggleIsFavoriteCommand = new Command<User>(user => { _realm.Write(() => user.IsFavorite = !user.IsFavorite); }); } }  <ViewCell> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="60" /> </Grid.ColumnDefinitions> </Grid> <Label Text="{Binding Name}"/> <Button Text="☆" Grid.Column="1" Command="{Binding Path=BindingContext.ToggleIsFavoriteCommand, Source={x:Reference Name=ContactsListView}}" CommandParameter="{Binding .}"/> </ViewCell> 
PropertyChanged or CollectionChanged and stop as soon as the subscription stops. This means that if you have a million objects processed and you do not want to receive notifications about them, then the possibility of observing RealmObjects will not in any way affect you. And if you care about changes and use RealmObjects for data binding, then the slowdown caused by notifications will be negligible compared to the logic of data binding and layout calculations that are performed by Xamarin Forms with each update. Alexander Alekseev - Xamarin-developer, freelancer. Works with the .NET platform since 2012. Participated in the development of a procurement automation system at Digamma. C 2015 went into freelancing and switched to mobile development using Xamarin. Currently working at StecPoint on an iOS application.
 Alexander Alekseev - Xamarin-developer, freelancer. Works with the .NET platform since 2012. Participated in the development of a procurement automation system at Digamma. C 2015 went into freelancing and switched to mobile development using Xamarin. Currently working at StecPoint on an iOS application.Source: https://habr.com/ru/post/320338/
All Articles