c# - Race condition between call return and OnNavigated events of page -
i building application using ibm mobilefirst platform foundation 6.3 windows phone 8 , have flow this.
in page user taps button , navigates page b. in page b user taps button , call initiated. if call returns user taken page c.
page corresponds viewmodel avm , other viewmodels bvm , cvm. call in page b initiated command in bvm , return handled there.
the problem flow. user initiates call on page b, before call returns, presses , navigates page a. bvm still exists when call returns user taken page c page a. not permitted behavior.
unfortunately mobilefirst not support cancelling requests cannot when navigating page.
what have tried: setting bool variable in bvm determines whether page b active page when call returns , updating on onnavigatedto , onnavigatedfrom events of page, creates race condition. if call returns while navigation occuring not if variable set in time. same goes change can on navigation events.
do have suggestion how can face problem. maybe way dispose of view model in time before call returns.
edit: more specific data , code. in page b bvm has list list<addonitemviewmodel> availableaddonvms
list displayed on screen , command invoces call in every addonitemviewmodel
code onnavigatedfrom of page b
var vm = this.datacontext addonsviewmodel; if (e.navigationmode == navigationmode.back) { foreach (var addonvm in vm.availableaddonvms) { addonvm.isstillonpage = false; system.diagnostics.debug.writeline("go back" + addonvm.addon.servicename + addonvm.isstillonpage); } } base.onnavigatingfrom(e);
code addonitemviewmodel
#region check if still on page public bool isstillonpage = true; #endregion ... //callback function public void afterenablecheck(){ deployment.current.dispatcher.begininvoke(() => { if (!isstillonpage) { system.diagnostics.debug.writeline("not on page now"); return; } // else continue navigation system.diagnostics.debug.writeline("continue navigation"); } ); }
edit 2: sample flow , logs
flow: call initiated on page b -> pressed before returns , navigation page happens -> navigation page c happens, doesn't.
logs:
go servicename1 false
go servicename2 false
. . go servicenamex false
not on page (navigation page c doesn't happen)
or
continue navigation (navigation page c happens).
it strange eventhough isstillonpage set false on every addonitemviewmodel still value appears true when if condition evaluated
the boolean value way handle issue. can bypass racing condition switching ui thread after callback of request. don't know ibm mobilefirst, should like:
private void requestcallback() { dispatcher.begininvoke(() => { // here, in ui thread if (isactive) { // page still active, trigger navigation navigationservice.navigate(...); } }); }
the button press, onnavigatedto
, onnavigatedfrom
, , method invoked dispatcher executing in ui thread, don't have worry racing conditions.
Comments
Post a Comment