c# - MvvmCross ShowViewModel loading sequence -


i have sequencing problem in viewmodel, seems thread-related can't quite figure our what's going wrong or how "fix" fixing it.

i have viewmodel needs call async method load initial data. calling async method init method mvvmcross calls automatically. if loading fails reason want show error screen, calling showviewmodel inside method called init not produce expected result. showviewmodel called correctly, following in debugger appears errorviewmodel shows before original viewmodel / view has finished loading - loads doesn't appear, over-written original viewmodel.

here's simplified version of loading code:

public async task init() {     await loadinitialdataasync(); }  protected async task loadinitialdataasync() {     var loadresult = await loadsomestuffasync();     if (loadresult.isbadnews)     {         showviewmodel<errorviewmodel>();         return;     } } 

the mvxtrace logs tell me showviewmodel called on errorviewmodel after called on initial viewmodel, initial view shows, not errorview.

to "fix" can 1 of 2 things.

i can wrap call loadinitialdataasync in task.run:

await task.run(async () => {     await loadinitialdataasync(); }); 

or, can add small delay before inner showviewmodel call:

protected async task loadinitialdataasync() {     var loadresult = await loadsomestuffasync();     if (loadresult.isbadnews)     {         await task.delay(1);         showviewmodel<errorviewmodel>();         return;     } } 

either of these changes produces desired result - if bad thing happens during load, errorviewmodel shows view.

the problem is, don't trust fix because don't understand what's going wrong under proverbial hood, , therefore don't know how robust fix is. seems arbitrary timing thing break again @ point in future @ inconvenient time.

if understands mvvmcross internals enough this, i'd appreciate it!

the init method should little. init typically used copy navigation parameters passed showviewmodel<tviewmodel>(). start method intended viewmodel startup such calling loadinitialdataasync. please review app lifecycle documentation more information.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -