'UIKit Consistency error: you are calling a UIKit method in Xamarin iOS

I build Chat application in Xamarin combined with Firebase Realtime Database. I test the app on Android emulator, it works fine. However when I run the Xamarin iOS app, I test it very slowly, on average it takes about 25-30s to display the message content. And after that I get the error: UIKit.UIKitThreadAccessException: 'UIKit Consistency error: you are calling a UIKit method that can only be invoked from the UI thread.'.I have consulted the articles here, but I still can't solve the problem

public ObservableCollection<MyDatabaseRecord> DatabaseItems { get; set; } = new ObservableCollection<MyDatabaseRecord>();
public static string FirebaseClient = "https://xxxxxxxxxxxxxxxxxxxxxxxxx.firebasedatabase.app/";
public static string FrebaseSecret = "PzxmWhdeDb7WM1JQmxxxxxxxxxxxxxxxxxxxxx";
public FirebaseClient fc = new FirebaseClient(FirebaseClient,
                   new FirebaseOptions { AuthTokenAsyncFactory = () => Task.FromResult(FrebaseSecret) });
public string roomid { get; set; }
public DetailChats(string roomID)
{
    InitializeComponent();
    roomid = roomID;
    LoadChat();
}

protected async void LoadChat()
{
    if (roomid != "")
    {
        string nameroom = "";

        BindingContext = this;
        string userget = "12345678";

        Device.BeginInvokeOnMainThread(() =>
        {
            var FirebaseClient = fc
            .Child("RecordsChat").Child(roomid)
            .AsObservable<MyDatabaseRecord>()
            .Subscribe(async (dbevent) =>
            {
                if (dbevent.Object != null)
                {
                    if (dbevent.Object.sender_uid == userget || dbevent.Object.receiver_uid == userget)
                    {
                        dbevent.Object.BackgroundUser = "#2085d000";
                    }
                    else
                    {
                        dbevent.Object.BackgroundUser = "#fafafa";
                    }

                    DatabaseItems.Add(dbevent.Object);

                    if (dbevent.Object.receiver_uid == userget)
                    {
                        await UpdateStatus(false, true);

                        nameroom = dbevent.Object.sender_uid;
                    }
                    else
                    {
                        nameroom = dbevent.Object.receiver_uid;
                    }

                    var customerList = await apiServiceUserinfo.GetCustomersInfo(nameroom);
                    string nameget = customerList.NameStore;

                    title_chat.Text = nameget;
                }
                else
                {
                    string namestore = Preferences.Get("GetNameStoreChat", "namestore");

                    nameroom = namestore;

                    var customerList = await apiServiceUserinfo.GetCustomersInfo(nameroom);
                    string nameget = customerList.NameStore;

                    title_chat.Text = nameget;
                }
            });
        });
    }   
}

How can I solve the problem:

  1. Improved display of data content from Firebase Realtime Database. Where did I go wrong?
  2. Error: UIKit.UIKitThreadAccessException: 'UIKit Consistency error: you are calling a UIKit method that can only be invoked from the UI thread.'

Thanks for all the results



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source