Search code examples
c#androidxamarinandroid-asynctaskbackground-process

Continue execution of services in background while using app Xamarin Android


I have some data to insert in database when user login mobile app. As well as I have some other services calls too, which makes my app login worst. So here I want to know that how can I login immediately & continue execution of other data services in background.

However I am making async call which is doesn't seem to work. The service calls I am making is it running in background? or I can make it more batter

private async Task InsertPushKeys()
{       
    foreach (var item in SISConst.Mychildren)
    {
        pushInfo.OrganizationId = Convert.ToInt64(item.OrganizationID);
        pushInfo.OsType = "Android";
        pushInfo.ServerkeyPush = SISConst.LmgKey;
        var ignore = await logDAL.InsertPushInfo(pushInfo);
    }
}

Below line executing the service.

var ignore = await logDAL.InsertPushInfo(pushInfo);

Edit 1: These service I am calling inside login button in the same order

_btnSignUp.Click += async (s, e) =>
                    {
var loggedInUser = await uLogin.UserLogin(_inputName.Text.Trim(), _inputPassword.Text);
Task.Run(async () => { mychildList = await uLogin.BindMychildrenGridData(result.UserID); }).Wait(); 
await DeletePushKeys(loggedInUser.UserID);
InsertPushKeys();
                    };

Solution

  • Call your services inside Task.Run without using await keyword. Await is holding control until it complete total execution. It might give warning for you that use await keyword but you can ignore it.

    Task.Run(async () =>
    {       
         InsertPushKeys();
    });
    

    & don't try using .Wait()(until you need it) at the end of method it is holding your execution.