Reusing Map control causes map to freeze

7436
16
10-27-2014 09:06 AM
SimonFisher
Occasional Contributor II

In our Windows Store App (not a map centric app) we use the same map (and map control) on several different pages within the application.  This was done for several reasons, including performance, keeping the map state etc. and originally dealing with a memory leak in the map control when loading/unloading/recreating.  We do this by creating one generic map control and keeping it in the application wide state and reusing when necessary. We create the control and initialize it once and then add/remove the control to the xaml tree when navigating to/from a page that requires a map something like this;

ContentGrid.Children.Add(App.ArcGISMapCtrl); //ContentGrid is a xaml grid

and for removing;

ContentGrid.Children.Remove(App.ArcGISMapCtrl);

This has been working great for us during all the Beta releases but in this Final release we now get the following behavior;

The first time a page is loaded and we create the map everything works fine and as expected.  Any subsequent navigation to the same page or any other page with a map the page loads fine but the map control is frozen.  Everything else works on the page except the map, is just hung or something.  I can see it, but it is not allowing any interaction.

I can get the map working on all page loads if I re-create the Map control on every page navigation but this kind of defeats the point since everything has to be re-initialized, as well the maps state is lost and would have to be saved/reloaded each time.  The reusing of the map control has been working great for us as users only see some lag in the first map load, but after that maps are always ready right away and in the same state they left.

Was something major changed in the Final release that might affect this?  Any suggestions on what I could try differently?

Thanks,

Simon

Tags (4)
0 Kudos
16 Replies
dotMorten_esri
Esri Frequent Contributor

We can reproduce the issue you're describing. Unfortunately there's not a quick workaround for this.

However I suggest you keep your Map around and assign it to a new MapView on each page. That will save you a lot initialization steps and should make the map load a lot quicker as well. If there's any other state you want to retain (like extent), you can store that as well (in that case, update myMap.InitialViewport = new Viewpoint(myMapView.Extent) when you leave a page).

0 Kudos
SimonFisher
Occasional Contributor II

Thanks, I'll play around with your suggested workaround.  Unfortunately this will result in a lot of extra coding and possibly affect performance.  Do you think its likely this could be fixed in a future release?  Also, please let me know if you think of any other workarounds I could try.

Thanks again,

Simon

0 Kudos
dotMorten_esri
Esri Frequent Contributor

I hope we will be able to improve this - I do see the benefit (albeit it goes a lot against the MVVM pattern). Hanging on to UI controls in memory has it's share of caveats too, so it's not something I would promote unless really needed.

0 Kudos
SimonFisher
Occasional Contributor II

Agreed, hanging onto the control and reusing is not a common or best practices approach, but it seems to work very well for us in our particular scenario, especially for performance.  I'll see how it performs with the suggested changes and let you know, but if its not a difficult change on your end to fix it would definitely be appreciated in a future release.

Thanks again,

Simon

0 Kudos
SimonFisher
Occasional Contributor II

Is the issue here that the MapView object has to be newly created in xaml on every page load or else it freezes?

I have tried to deal with this in code behind because I am actually tying to hold on to my own custom control which in turn contains the MapView and Map objects.  Therefore I am doing something like this below, but it still freezes on second attempted view.

//when leaving page null MapView

this.MyMapView = null;

this.PreviousExtent = this.MyMapView.Extent;

//when entering page create new MapView on Custom Control and assign existing map and previous extent

this.MyMapView = new MapView();

this.MyMap.InitialViewpoint = new Viewpoint(this.PreviousExtent);

this.MyMapView.Map = this.MyMap;

Any other suggestions?

Thanks

Simon

0 Kudos
dotMorten_esri
Esri Frequent Contributor

UIElement caching is not recommended, and currently not supported by the MapView either. So yes the MapView should be recreated on each page. You can (and should) cache the map though. We're are looking at if we can improve this in the future to increase load speed from page to page. But be aware that this approach can create some significant memory consumption and hold on to a lot more objects than you might want to.

0 Kudos
RistoMoilanen
New Contributor

Hi Morten!

Do you mean, että using MapView on Page which has NavigationCacheMode=Required is not supported scenario?

We have same crashing problem mentioned here (Navigating between pages with Maps ) when navigation between Map Page and another Page.

We thought that NavigationCacheMode=Required could help with this issue, but that's not supported with page containing map view?

Risto

0 Kudos
SimonFisher
Occasional Contributor II

From the release notes at 10.2.6 it seems this issue has been fixed correct?  We can now safely reuse the map control? Or do you still suggest not doing this unless absolutely necessary?

0 Kudos
dotMorten_esri
Esri Frequent Contributor

It should be - are you saying it isn't working for you?

0 Kudos