I have a SceneView from a mobile scene package in a swiftUI app. When I add operational layers and rotate the device to landscape then back to portrait, I am consistently getting a crash with the following message:
"Fatal error: Object is already in use and may not be reused."
I have updated to 200.2 but still the error persists. I also get this error occasionally when entering background mode from the sceneView. Once again if I remove operational layers or select another tab in the navigationSplitView then there is no error.
This error only appears if I have an operational layer added.
The simple code to reproduce this is below:
enum Detail: Hashable {
case geography
}
struct ContentView: View {
@State private var selection: Detail?
var body: some View {
NavigationSplitView() {
List(selection: $selection) {
NavigationLink(value: Detail.geography) {
HStack(spacing: 12) {
Image(systemName: "globe")
Text("Geography")
}
}
}
} detail: {
switch selection {
case .geography:
GeographyView()
default:
EmptyView()
}
}
}
}
struct GeographyView: View {
@State private var scene = Scene()
@State private var mobileScenePackage: MobileScenePackage!
@State var camera: Camera? = Camera(location: Point(x: 113.915001, y: 22.308901, z: 4_953_394, spatialReference: .wgs84), heading: 360.0, pitch: 0.0, roll: 0)
@State var presentPositionOverlay: GraphicsOverlay?
var mapOverlays: [GraphicsOverlay] {
var overlays: [GraphicsOverlay] = []
if presentPositionOverlay != nil {
overlays.append(presentPositionOverlay!)
}
return overlays
}
private func loadMobileScenePackage() async throws {
let sceneURL = Bundle.main.url(forResource: "satteliteImageryScene", withExtension: "mspk")!
self.mobileScenePackage = MobileScenePackage(fileURL: sceneURL)
try await mobileScenePackage.load()
guard let scene = mobileScenePackage.scenes.first else { return }
let x = 113.915001
let y = 22.308901
scene.initialViewpoint = Viewpoint(latitude: y, longitude: x, scale: 4_953_394)
setPresentPositionOverlay(x: x, y: y)
self.scene = scene
}
func setPresentPositionOverlay(x: Double, y: Double) {
let overlay = GraphicsOverlay()
let location = Point(x: x, y: y, spatialReference: .wgs84)
let backgroundSymbol = SimpleMarkerSymbol(style: SimpleMarkerSymbol.Style.circle, color: .white, size: 14)
let markerSymbol = SimpleMarkerSymbol(style: SimpleMarkerSymbol.Style.circle, color: .blue, size: 10)
let symbol = CompositeSymbol(symbols: [backgroundSymbol, markerSymbol])
let graphic = Graphic(geometry: location, symbol: symbol)
graphic.zIndex = 100
overlay.addGraphic(graphic)
presentPositionOverlay = overlay
}
// MARK: - Body
var body: some View {
NavigationStack {
SceneViewReader { sceneView in
ZStack {
SceneView(scene: scene, camera: $camera, graphicsOverlays: mapOverlays)
.task {
do {
try await loadMobileScenePackage()
} catch {
print(error)
}
}
}
}
}
}
}
Want to let you know we are looking into this question. There seems to be 2 problems, one with the navigation split view, and the other with the way of using operational layers in your code. Will loop back when I have more details.
Thanks. This was for example purposes. In my app the mapOverlay variable and camera are in a view model. But the method of adding the overlay is the same so please let me know if it's' not correct. And awaiting the reply regarding the error and Nav Split View.
Out of curiosity, do you happen to have installed the Xcode 15.0 Beta 7 and iOS 17.0 Simulator?
We are trying to narrow down the issue and would like to know if it is still a crash on iOS 17. Thank you!
I should have mentioned the crash occurs on iPad and iPhone max that use the navigation drawer both simulator and actual device iOS 16.6
I’ll try iOS 17 beta and get back to you.