Skip to content

Commit 1381dd8

Browse files
authored
Updated data manager to handle no internet connection (#1065)
1 parent 5ed0db2 commit 1381dd8

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

src/ArcGISRuntime.Samples.Shared/Managers/DataManager.cs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,29 @@ private static void CancelDownload(FileDownloadTask downloadTask)
8787
/// </summary>
8888
/// <param name="item">The portal item to check.</param>
8989
/// <returns><c>true</c> if data is available and up-to-date, false otherwise.</returns>
90-
private static bool IsDataPresent(PortalItem item)
90+
private static async Task<bool> IsDataPresent(string itemId)
9191
{
9292
// Look for __sample.config file. Return false if not present.
93-
string configPath = Path.Combine(GetDataFolder(item.ItemId), "__sample.config");
93+
string configPath = Path.Combine(GetDataFolder(itemId), "__sample.config");
9494
if (!File.Exists(configPath)) { return false; }
9595

9696
// Get the last write date from the __sample.config file metadata.
9797
DateTime downloadDate = File.GetLastWriteTime(configPath);
9898

99-
// Return true if the item was downloaded after it was last modified.
100-
return downloadDate >= item.Modified;
99+
try
100+
{
101+
// Create ArcGIS portal item
102+
var portal = await ArcGISPortal.CreateAsync().ConfigureAwait(false);
103+
var item = await PortalItem.CreateAsync(portal, itemId).ConfigureAwait(false);
104+
// Return true if the item was downloaded after it was last modified.
105+
return downloadDate >= item.Modified;
106+
}
107+
// Catch exception when data manager cant access the internet.
108+
catch (Exception ex)
109+
{
110+
System.Diagnostics.Debug.WriteLine(ex.Message);
111+
return true;
112+
}
101113
}
102114

103115
public static async Task EnsureSampleDataPresent(SampleInfo info, Action<ProgressInfo> onProgress = null)
@@ -137,12 +149,14 @@ public static async Task EnsureSampleDataPresent(IEnumerable<string> itemIds, Ca
137149
int id = 0;
138150
foreach (string itemId in itemIds)
139151
{
140-
// Create ArcGIS portal item
141-
var portal = await ArcGISPortal.CreateAsync(token).ConfigureAwait(false);
142-
var item = await PortalItem.CreateAsync(portal, itemId, token).ConfigureAwait(false);
152+
bool isDownloaded = await IsDataPresent(itemId);
143153
// Download item if not already present
144-
if (!IsDataPresent(item))
154+
if (!isDownloaded)
145155
{
156+
// Create ArcGIS portal item
157+
var portal = await ArcGISPortal.CreateAsync(token).ConfigureAwait(false);
158+
var item = await PortalItem.CreateAsync(portal, itemId, token).ConfigureAwait(false);
159+
146160
var index = id;
147161
Action<ProgressInfo> action = (info) => combinedProgress(info, index);
148162
Task downloadTask = DownloadItem(item, token, combinedProgress is null ? null : action);
@@ -161,12 +175,14 @@ public static Task DownloadDataItem(string itemId)
161175

162176
public static async Task DownloadDataItem(string itemId, CancellationToken cancellationToken, Action<ProgressInfo> onProgress = null)
163177
{
164-
// Create ArcGIS portal item
165-
var portal = await ArcGISPortal.CreateAsync(cancellationToken).ConfigureAwait(false);
166-
var item = await PortalItem.CreateAsync(portal, itemId, cancellationToken).ConfigureAwait(false);
178+
bool isDownloaded = await IsDataPresent(itemId);
167179
// Download item if not already present
168-
if (!IsDataPresent(item))
180+
if (!isDownloaded)
169181
{
182+
// Create ArcGIS portal item
183+
var portal = await ArcGISPortal.CreateAsync(cancellationToken).ConfigureAwait(false);
184+
var item = await PortalItem.CreateAsync(portal, itemId, cancellationToken).ConfigureAwait(false);
185+
170186
await DownloadItem(item, cancellationToken, onProgress);
171187
}
172188
}

0 commit comments

Comments
 (0)