Developing with feature services
The ability to include feature services in mobile applications means that the same feature services used in web applications can be consumed in mobile applications. Similar to mobile services that allow data to be stored locally on a device for disconnected uses including editing, feature services can be consumed by a mobile application. The feature service is treated as a feature source, the same way as other operational layers. The feature service is used to download data to a device, which stores the data in a local mobile cache. The mobile application can then use this mobile cache in a disconnected environment to display, query, and update the data. These capabilities have been written to minimize the impact on current applications by using the existing mobile cache for storage, implementing this functionality as a feature source that follows the existing behavior, and acts like the other map layers.
See Publishing a hosted feature service for details on how to correctly establish the feature service in your mobile applications.
The feature service functionality has been implemented using two classes: FeatureServiceReplicaManager and FeatureServiceReplicaResults. The creation of these two classes means that the methods used to interact with a feature service have been centralized within these classes, and other classes that consume this data do not require additional customization.
The FeatureServiceReplicaManager class has some key required properties that must be set including the following:
- Url (the uniform resource locator [URL] address of the feature service)
- MobileCache (the mobilecache for local data storage)
- TokenCredential ( the username and password used for the service)
//FeatureService Connection using replicamanager
string featServiceUrl = @"http://services.arcgis.com/<hostedservice>/arcgis/rest/services/<myservice/FeatureServer";
FeatureServiceReplicaManager fsreplica = new FeatureServiceReplicaManager();
//MobileCache
string cacheStoragePath = @"C:\COGS\data\FSCache";
MobileCache mobileCache = new MobileCache(cacheStoragePath);
//delete old cache
if (mobileCache.CacheExists == true)
mobileCache.DeleteCache();
////////////////////////////
//Create a token to access the service with valid username\password
TokenCredential token = new TokenCredential("Username", "PassWord");
//Set feature service properties
fsreplica.TokenCredential = token;
fsreplica.Url = featServiceUrl;
fsreplica.Initialize();
fsreplica.MobileCache = mobileCache;
//Get the data from the service to the local cache
fsreplica.CreateReplica();
//Open the cache and add it to the maplayers
mobileCache.Open();
if (!fsreplica.IsRegistered )
fsreplica.RegisterReplica();
map1.MapLayers.Add(mobileCache);