0
votes

How to load the view page before executing any functions?

App Process:
1. User will click login button
2. I will navigate to sync page
3. sync page will execute six (6) functions after executing 6 functions it will redirect to my main menu page

The problem is with my number 1 and 2 process. When the user click the login button it will be stuck in my login page then go directly to my main menu page. How can I see the sync page before redirecting to main menu page.

public SyncPage (string host, string database, string contactID)
        {
            InitializeComponent();
            new Task(SyncSetup).Start();
        }

    async void SyncSetup()
    {
        Deletedata();
        InsertUserData(host, database, contactID);
        InsertCAFData(host, database, contactID);
        InsertContactsData(host, database, contactID);
        InsertActivityData(host, database, contactID);
        Device.BeginInvokeOnMainThread(() =>
        {
            // Assuming this function needs to use Main/UI thread to move to your "Main Menu" Page
            OnSyncComplete(host, database, contactID);
        });
    }   
1

1 Answers

2
votes

I would move those functions/processes to a separate thread/task, a super easy way is to use a fire & forget Task and have that task handle those setup processes and then move back to the UI thread in order to move your "Main Menu" page.

Something like this:

string contactID; string host; string database;
public SyncPage (string contactID, string host, string database)
{
    InitializeComponent();
    this.contectID = contactID; this.host = host; this.database = database;
    new Task(SyncSetup).Start(); // Or do this in OnAppearing override
}

protected override void OnAppearing()
{
    base.OnAppearing();
    new Task(SyncSetup).Start(); // This could be an await'd task if need be
}

async void SyncSetup()
{
    Deletedata();
    InsertUserData(host, database, contactID);
    InsertCAFData(host, database, contactID);
    InsertContactsData(host, database, contactID);
    InsertActivityData(host, database, contactID);
    Device.BeginInvokeOnMainThread(() =>
    {
        // Assuming this function needs to use Main/UI thread to move to your "Main Menu" Page
        OnSyncComplete(host, database, contactID);
    });
}