Realm accessed from incorrect thread Xamarin forms
У меня есть Page.xaml.cs, в котором содержится код
CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
{
_viewModel.LoadItemsCommand.Execute(s);
};
ViewModel содержит переменную realm и команду LoadItemsCommand
protected readonly Realm realm;
public ObservableCollection<Topic> Items { get; }
public Command LoadItemsCommand { get; }
public ItemsViewModel()
{
Items = new ObservableCollection<Topic>();
LoadItemsCommand = new Command(() => ExecuteLoadItemsCommand());
}
private async void ExecuteLoadItemsCommand()
{
await GetNewTopics();
Items.Clear();
var items = realm.All<Topic>().ToList().OrderByDescending(_ => _.UserMessage.Last().CreateDt);
foreach (var item in items)
Items.Add(item);
}
При попытке доступа к Realm другого потока возникает ошибка. Как нужно действовать в таком случае?