Toggle between feature request modes

View inMAUIWPFWinUIView on GitHub

Use different feature request modes to populate the map from a service feature table.

Image of toggle between feature request modes

Use case

ServiceFeatureTable supports three request modes, which define how features are requested from the service and stored in the local table. The feature request modes have different performance characteristics. Use On interaction cache in scenarios with large amounts of infrequently edited data. Use manual cache in scenarios where you want to explicitly control requests for features. Use no cache in scenarios where you always want the freshest data.

How to use the sample

Run the sample and use the radio buttons to change what feature request modes you want to use (the default value is on interaction cache). After you selected which feature request mode to use, click the Populate button to apply the feature request mode.

How it works

  1. Create a ServiceFeatureTable with the a feature service URL.
  2. Set the FeatureRequestMode property of the service feature table to the desired mode (OnInteractionCache, OnInteractionNoCache, or ManualCache).
    • If using ManualCache, populate the features with ServiceFeatureTable.PopulateFromServiceAsync().
  3. Create a FeatureLayer with the feature table and add it to an MapView.Map's operational layers to display it.

Relevant API

  • FeatureLayer
  • FeatureRequestMode.ManualCache
  • FeatureRequestMode.NoCache
  • FeatureRequestMode.OnInteractionCache
  • ServiceFeatureTable
  • ServiceFeatureTable.FeatureRequestMode
  • ServiceFeatureTable.PopulateFromServiceAsync

About the data

This sample uses the Trees of Portland service showcasing over 200,000 street trees in Portland, OR. Each tree point models the health of the tree (green - better, red - worse) as well as the diameter of its trunk.

Additional information

On interaction cache is the default feature request mode. Features are requested automatically for the visible extent as the users pans and zooms the map. If the user returns to an area where features have previously been requested, those features won't be requested again. In no cache mode, features are automatically populated from the service for the visible extent. Each time the user pans and zooms, features are downloaded for the visible extent. Features are still cached in a local geodatabase for display, but the cache will always be populated with the latest data after navigation. In manual cache mode, features are never automatically populated from the service. All features are loaded manually using calls to PopulateFromServiceAsync.

Tags

cache, feature request mode, performance

Sample Code

ToggleBetweenFeatureRequestModes.xaml.csToggleBetweenFeatureRequestModes.xaml.csToggleBetweenFeatureRequestModes.xaml
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2022 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.

using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Mapping;
using Microsoft.UI.Xaml;
using System;
using System.Threading.Tasks;

namespace ArcGIS.WinUI.Samples.ToggleBetweenFeatureRequestModes
{
    [ArcGIS.Samples.Shared.Attributes.Sample(
        name: "Toggle between feature request modes",
        category: "Data",
        description: "Use different feature request modes to populate the map from a service feature table.",
        instructions: "Run the sample and use the radio buttons to change what feature request modes you want to use (the default value is  **on interaction cache**). After you selected which feature request mode to use, click the `Populate` button to apply the feature request mode. ",
        tags: new[] { "cache", "feature request mode", "performance" })]
    public partial class ToggleBetweenFeatureRequestModes
    {
        private ServiceFeatureTable _treeFeatureTable;
        private FeatureLayer _treeFeatureLayer;

        public ToggleBetweenFeatureRequestModes()
        {
            InitializeComponent();
            _ = Initialize();
        }

        private async Task Initialize()
        {
            try
            {
                // Create new Map with basemap.
                Map myMap = new Map(BasemapStyle.ArcGISTopographic);
                MyMapView.Map = myMap;

                // Set intial map location.
                MyMapView.Map.InitialViewpoint = new Viewpoint(45.5266, -122.6219, 6000);

                // Create uri to the used feature service.
                Uri serviceUri = new Uri(
                   "https://services2.arcgis.com/ZQgQTuoyBrtmoGdP/arcgis/rest/services/Trees_of_Portland/FeatureServer/0");

                // Create a feature table for the trees of Portland feature service.
                // The feature request mode for this service feature table is OnInteractionCache by default.
                _treeFeatureTable = new ServiceFeatureTable(serviceUri);

                // Create FeatureLayer that uses the created table.
                _treeFeatureLayer = new FeatureLayer(_treeFeatureTable);

                // Add created layer to the map.
                MyMapView.Map.OperationalLayers.Add(_treeFeatureLayer);
            }
            catch (Exception ex)
            {
                await new MessageDialog2(ex.Message).ShowAsync();
            }
        }

        // Use this method for manual cache.
        private async Task FetchCacheManually()
        {
            // Create new query object that contains parameters to query specific request types.
            QueryParameters queryParameters = new QueryParameters()
            {
                Geometry = MyMapView.VisibleArea
            };

            // Create list of the fields that are returned from the service.
            // Using "*" will return all fields. This can be replaced to return certain fields.
            string[] outputFields = { "*" };

            try
            {
                // Populate feature table with the data based on query.
                await _treeFeatureTable.PopulateFromServiceAsync(queryParameters, true, outputFields);
            }
            catch (Exception ex)
            {
                await new MessageDialog2(ex.Message).ShowAsync();
            }
        }

        private void PopulateButtonClick(object sender, RoutedEventArgs e)
        {
            _ = FetchCacheManually();
        }

        private void CacheChecked(object sender, RoutedEventArgs e)
        {
            // Populates the map with server feature table request mode OnInteractionCache.
            // Features are requested automatically for the visible extent. If the area is visited again, the features won't be requested again.
            _treeFeatureTable.FeatureRequestMode = FeatureRequestMode.OnInteractionCache;

            // Disable populate map button used for manual cache.
            PopulateMap.IsEnabled = false;
        }

        private void NoCacheChecked(object sender, RoutedEventArgs e)
        {
            // Populates the map with server feature table request mode OnInteractionNoCache.
            // Features are downloaded for the visible extent. If the area is visited again, the cache will be populated with the latest data.
            _treeFeatureTable.FeatureRequestMode = FeatureRequestMode.OnInteractionNoCache;

            // Disable populate map button used for manual cache.
            PopulateMap.IsEnabled = false;
        }

        private void ManualCacheChecked(object sender, RoutedEventArgs e)
        {
            // Populates the map with server feature table request mode ManualCache.
            // Features are never automatically populated from the services. All features are loaded manually using PopulateFromServiceAsync.
            _treeFeatureTable.FeatureRequestMode = FeatureRequestMode.ManualCache;

            // Enable populate map button used for manual cache.
            PopulateMap.IsEnabled = true;
        }
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.