question

guessmeifucan-8050 avatar image
0 Votes"
guessmeifucan-8050 asked RoyLi-MSFT commented

Viewmodel data are lost after i receive response from async api call

In my scenario, i have a app with navigation view. navigation view holds multiple navigation item. each navigation holds its own Page. Each page contains its own viewmodel. The Viewmodel contains multiple objects. The ViewModel is responsible for hitting an API and process its response to the objects.

In my case, If the navigating the page faster then Page 1 opens API 1 triggers (response not yet received) then Page 2 opens and API 2 triggers (response not yet received) and continues. In such case, when the API 1 response is returned after some time, all the objects in my viewmodel is cleared (viewmodel data was cleared on page navigation to next page) and hence the exception is thrown.

My question is, if i navigate to another page, am i able to stop the API call of my last page or kill it without returning back to viewmodel ?

dotnet-csharpwindows-uwp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@guessmeifucan-8050 Does Nico's reply make sense? If Nico's reply helps you solve the issue, please consider accepting it as answer. This could help others who have the same issue.

0 Votes 0 ·

1 Answer

NicoZhu-MSFT avatar image
0 Votes"
NicoZhu-MSFT answered NicoZhu-MSFT commented

Hello, Welcome to Micorosoft Q&A,

when the API 1 response is returned after some time, all the objects in my viewmodel is cleared

it is by design that all the objects in my viewmodel is cleared when navigate from. You could try to set the page NavigationCacheMode as Required to cache current page when navigate from.

 <Page
     x:Class="WebViewGif.MainPage"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     xmlns:local="using:WebViewGif"
     NavigationCacheMode="Required"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
     mc:Ignorable="d">


For more please refer this document.


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

is this recommended to use NavigationCacheMode ? As I don't need any existing page details and there are many navigation items in my app, I intentionally didn't use it. Using it may cache a lot and lot of objects for my case and also those are not required for my future too. @NicoZhu-MSFT

0 Votes 0 ·

Yep, it is recommended way to cache the specific page that contains big data. for your scenario, the good practice is that cancel this request when the page navigate from. if you do want continue this request when the page navigate from. we suggest you separate data request from viewmodel. even the viewmodel is recycled, data request will be continue, you could store the received data into local storage, when page reload, you could load it from local storage directly.

0 Votes 0 ·