Here is a method that toggles the state of a boolean variable called imageryOn.
When I tap a button the code runs, and I am able to set the first object in the Operational Layers array as a ArcGISTiledLayer. Cool, tapping the on screen button shows the satellite imagery hosted service on the map and sets it to Operational Layers at index 0.
When I toggle the imageryOn bool to false I get an error message when trying to remove the first operational layer:
Thread 1: Fatal error: Object is already in use and may not be reused.
However, if I used the removeAllOperationalLayers method I am able to remove all operational layers (including the imagery layer). And the toggle then just shows imagery on and then imagery off.
func toggleImagery(){
imageryOn = !imageryOn
print(map.operationalLayers)
if imageryOn {
let imageryLayer = ArcGISTiledLayer(url: URL(string: "https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer")!)
map.insertOperationalLayer(imageryLayer, at: 0)
print(map.operationalLayers.first!)
print(map.operationalLayers)
} else {
map.removeOperationalLayer(map.operationalLayers[0])
//map.removeAllOperationalLayers()
}
}
Any ideas on what could be going on here?
Thanks