How to clear Map Operational Layers to remove geodatabase file lock

417
2
09-04-2023 06:47 AM
Labels (2)
MartinMaps
New Contributor

Hi all,

I was hoping to get some advice regarding feature layers and Geodatabase objects.

In our software we have a Map which loads and then displays feature layers. We need a process for updating the data behind the feature layers while the program is running (a Geodatabase file). We do this by clearing the OperationalLayers of the Map object. This removes the feature layers.

The problem we have is that clearing the OperationalLayers does not remove the file lock on the Geodatabase file immediately. Instead, this can take quite some time, normally until the next Garbage Collector (GC) run.

We have tried forcing a GC collection; however, this is not guaranteed to clear the file lock before we need to update the file.

We have also attempted using a Geodatabase.Close() method call, but this does not seem to have any effect beyond that of the OperationalLayers.Clear() method.

It would be great if anyone knows of a way we can clear the OperationalLayers (feature layers) and remove the file lock immediately (or awaitable) from the Geodatabase file. Currently our solution is to force a GC collection and run Thread.Sleep() for a second, which is not ideal.

In our code, feature layers are added directly to the Map’s OperationalLayers. The Geodatabase object is a local variable and should be out of scope after the feature layer data is loaded and before being passed to the Map’s OperationalLayers.

Code:

public Map Map

private void RemoveAllFeatureLayers()
        {
            Map.OperationalLayers.Clear();

            GC.Collect();

            Thread.Sleep(1000);
        }

Using the following:

  • Esri.ArcGISRuntime - Nuget package v100.15.0
  • Esri.ArcGISRuntime.UWP - Nuget package v100.15.0
  • C# - UWP .Net Standard 2.0
  • MVVM model (so ideally, we want to avoid solutions that make use of a view's code behind)
0 Kudos
2 Replies
JoeHershman
MVP Regular Contributor

Seems to me that it may not be elegant, but if adding the time delay is working than it's a good solution.   Although, you could try adding the WaitForPendingFinalizers call, and perhaps not need the delay.

Create an extension method on Map and then you can just do in cleaner looking code from any VM

public static class MapExtensions
{
	public static async Task Close(this Map map)
	{
		map.OperationalLayers.Clear();

		GC.Collect();
		GC.WaitForPendingFinalizers();

		await Task.Delay(1000);
	}
}

Then just use from your VM

await map.Close();

 

Thanks,
-Joe
0 Kudos
MartinMaps
New Contributor

Thanks for the information. We did look at GC.WaitForPendingFinalizers() previously, but were put off by the fact that it may not complete (according to the Microsoft docs).

The extension method is a nice touch.

While it does work, we're still hoping there's an ESRI solution to the problem. It would be nice to either rule out or be able to make use of an ESRI method, rather than relying on the GC which isn't always predictable.