Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

The application called an interface that was marshalled for a different thread.

shiwamshiwam Member ✭✭
edited April 2020 in Xamarin.Forms

i got error when i want to add the data into observablecollection..actually i want to implement realtime changes so i used AsObservable<>()..data is stored into database but while Updating the UI.How can i achieve that pls help me with it.

  public ObservableCollection<Person> data;
    public ObservableCollection<Person> Data { get { return data; } set { data = value; NotiPropertyChanged("Data"); } }
    public IDisposable returnevents;
   private async void addlist()
    {
        await firebase.Child("Persons").PostAsync(new Person
        {
            Name =this.Name,
        });
        returnevents = firebase.Child("Persons").AsObservable<Person>().Subscribe(eventReceived =>
        {
            if (eventReceived.EventType == Firebase.Database.Streaming.FirebaseEventType.InsertOrUpdate)
            {
                var IorUitem = Data.FirstOrDefault(i => i.Name == eventReceived.Object.Name);
                if (IorUitem == null)
                {
                    Data.Add(new Person { Name = eventReceived.Object.Name });//here i got a error i.e System.Runtime.InteropServices.COMException: 'The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))'
                }
               ~~~~
            }
        });
                       ////////////////main page  //////////////////
                      /////delete the item from firebase but observableCollection is not notify means mot updating the UI////
   private async void DeleteMenuItem_Clicked(object sender, EventArgs e)
    {
        var vm = this.BindingContext as BaseViewmodel;
        var item = sender as MenuItem;
        var deleteitem = item.BindingContext as Person;
        var items = (await firebase
                          .Child("Persons")
                              .OnceAsync<Person>()).Where(a => a.Object.Name == deleteitem.Name).FirstOrDefault();
        await firebase.Child("Persons").Child(items.Key).DeleteAsync();
        vm?.Delete.CanExecute(deleteitem);
    }

///////////////////////in vm deletecommand///////////////////
public Command Delete
{
get
{
return new Command((Person) =>
{

                returnevents = firebase.Child("Persons").AsObservable<Person>().Subscribe(eventReceived =>
                {
                    if (eventReceived.EventType == Firebase.Database.Streaming.FirebaseEventType.Delete)
                    {
                        Data.Remove(eventReceived.Object);
                    }
                });


            });
        }
    }
Sign In or Register to comment.