<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Reusing Map etc on two MapViews in Swift Maps SDK Questions</title>
    <link>https://community.esri.com/t5/swift-maps-sdk-questions/reusing-map-etc-on-two-mapviews/m-p/1382421#M216</link>
    <description>&lt;P&gt;Unfortunately at this time, I don't see a great solution to your problem that doesn't involve a hack with introducing a delay in showing the second MapView when the size class changes. That hack might be enough of a workaround for you to continue development. If you really needed it, I can clean up a prototype and get that to you. Please let me know.&lt;/P&gt;&lt;P&gt;However, the good news is that we will have a fix for your scenario in our April release (200.4).&lt;/P&gt;</description>
    <pubDate>Wed, 14 Feb 2024 19:05:54 GMT</pubDate>
    <dc:creator>rolson_esri</dc:creator>
    <dc:date>2024-02-14T19:05:54Z</dc:date>
    <item>
      <title>Reusing Map etc on two MapViews</title>
      <link>https://community.esri.com/t5/swift-maps-sdk-questions/reusing-map-etc-on-two-mapviews/m-p/1381530#M213</link>
      <description>&lt;P&gt;I have a view that uses tab tab for iPhone layout (aka. "compact" size class) and a full-screen map with a floating overlay in lieu of the tab bar on iPad (aka. "regular" size class).&lt;/P&gt;&lt;P&gt;The map view's state (map, overlays, viewpoint, etc) are handled by an ObservableObject.&lt;/P&gt;&lt;P&gt;However, when the user puts the app in "slide over" or "split view" modes on iPad, the size class changes from "regular" to "compact", causing SwiftUI to re-render the hierarchy, destroy the old map view, and create a brand new MapView. This new mapview is then handed the same coordinator object.&lt;/P&gt;&lt;P&gt;This causes all kinds of trouble, since it seems most of the stuff are classes/reference types and it seems MapView does't support reusing those for more than a single MapView, methinks.&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;If the mapview has overlays, I get a crash with "Object is already in use and may not be reused".&lt;/LI&gt;&lt;LI&gt;If the mapview doesn't have overlays, the mapview won't crash, but also won't display the map after the size class change&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;I do want the app to preserve viewpoint and other state when changing size class.&lt;/P&gt;&lt;P&gt;Example:&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="swift"&gt;struct ContentView: View {
    @Environment(\.horizontalSizeClass) private var sizeClass
    @StateObject private var coordinator = MapCoordinator()

    var body: some View {
        if sizeClass == .regular {
            MyMapView(coordinator: coordinator)
                .overlay(alignment: .topLeading) { Menu() }
        } else {
            TabView {
                Menu()
                    .tabItem { Text("Menu") }
                MyMapView(coordinator)
                    .tabItem { Text("Map") }
            }
        }
    }
}

struct MyMapView: View {
    @ObservedObject var coordinator: MapCoordinator
    
    var body: some View {
        MapView(
            map: coordinator.map, 
            viewpoint: coordinator.viewPoint, 
            graphicsOverlays: coordinator.graphicsOverlays
        )
        .onScaleChanged(perform: coordinator.scaleChanged)
        .onRotationChanged(perform: coordinator.rotationChanged)
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Are my assumptions correct? Can I rework my app to fix these issues?&lt;/P&gt;</description>
      <pubDate>Tue, 13 Feb 2024 14:00:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/swift-maps-sdk-questions/reusing-map-etc-on-two-mapviews/m-p/1381530#M213</guid>
      <dc:creator>sveinhal</dc:creator>
      <dc:date>2024-02-13T14:00:22Z</dc:date>
    </item>
    <item>
      <title>Re: Reusing Map etc on two MapViews</title>
      <link>https://community.esri.com/t5/swift-maps-sdk-questions/reusing-map-etc-on-two-mapviews/m-p/1381673#M214</link>
      <description>&lt;P&gt;This is a class of issue that we've recently come up against. Thank you for explaining your specific scenario. We will dig into this a bit and get back to you.&lt;/P&gt;</description>
      <pubDate>Tue, 13 Feb 2024 16:50:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/swift-maps-sdk-questions/reusing-map-etc-on-two-mapviews/m-p/1381673#M214</guid>
      <dc:creator>rolson_esri</dc:creator>
      <dc:date>2024-02-13T16:50:17Z</dc:date>
    </item>
    <item>
      <title>Re: Reusing Map etc on two MapViews</title>
      <link>https://community.esri.com/t5/swift-maps-sdk-questions/reusing-map-etc-on-two-mapviews/m-p/1382421#M216</link>
      <description>&lt;P&gt;Unfortunately at this time, I don't see a great solution to your problem that doesn't involve a hack with introducing a delay in showing the second MapView when the size class changes. That hack might be enough of a workaround for you to continue development. If you really needed it, I can clean up a prototype and get that to you. Please let me know.&lt;/P&gt;&lt;P&gt;However, the good news is that we will have a fix for your scenario in our April release (200.4).&lt;/P&gt;</description>
      <pubDate>Wed, 14 Feb 2024 19:05:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/swift-maps-sdk-questions/reusing-map-etc-on-two-mapviews/m-p/1382421#M216</guid>
      <dc:creator>rolson_esri</dc:creator>
      <dc:date>2024-02-14T19:05:54Z</dc:date>
    </item>
    <item>
      <title>Re: Reusing Map etc on two MapViews</title>
      <link>https://community.esri.com/t5/swift-maps-sdk-questions/reusing-map-etc-on-two-mapviews/m-p/1382695#M219</link>
      <description>&lt;P&gt;Thanks for you suggestion, and super thanks for fixing this in the upcoming release!&lt;/P&gt;&lt;P&gt;I'll see if I can get it working with a little delayed view, or by perhaps cloning the data in my model on view life cycle events.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Feb 2024 08:14:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/swift-maps-sdk-questions/reusing-map-etc-on-two-mapviews/m-p/1382695#M219</guid>
      <dc:creator>sveinhal</dc:creator>
      <dc:date>2024-02-15T08:14:50Z</dc:date>
    </item>
    <item>
      <title>Re: Reusing Map etc on two MapViews</title>
      <link>https://community.esri.com/t5/swift-maps-sdk-questions/reusing-map-etc-on-two-mapviews/m-p/1382945#M220</link>
      <description>&lt;P&gt;As&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/482814"&gt;@rolson_esri&lt;/a&gt; said, we will have a fix for the bug in a couple of months, but in the meantime, you can probably refactor your code a little bit to get by. In SwiftUI, it is generally a good rule of thumb to preserve a view’s structural identity where possible. This means putting conditionals in view modifiers instead of using if/else statements. In your case, you can do this by hiding the tab bar using &lt;A href="https://developer.apple.com/documentation/swiftui/view/toolbar(_:for:)" target="_self"&gt;&lt;EM&gt;toolbar(_:for:)&lt;/EM&gt;&lt;/A&gt;&amp;nbsp; and showing the &lt;EM&gt;Menu&lt;/EM&gt; in the &lt;EM&gt;overlay&lt;/EM&gt; when &lt;EM&gt;sizeCase == .regular&lt;/EM&gt;. That way, you just get the effect you were looking for while only using one &lt;EM&gt;MapView&lt;/EM&gt;. Let me know if this works for you!&lt;/P&gt;&lt;LI-CODE lang="swift"&gt;struct ContentView: View {
    @Environment(\.horizontalSizeClass) private var sizeClass
    @StateObject private var coordinator = MapCoordinator()
    
    var body: some View {
        TabView {
            Menu()
                .tabItem { Text("Menu") }
            MyMapView(coordinator: coordinator)
                .tabItem { Text("Map") }
                .toolbar(sizeClass == .regular ? .hidden : .automatic, for: .tabBar)
                .overlay(alignment: .topLeading) {
                    if sizeClass == .regular {
                        Menu()
                    }
                }
        }
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Feb 2024 17:38:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/swift-maps-sdk-questions/reusing-map-etc-on-two-mapviews/m-p/1382945#M220</guid>
      <dc:creator>CalebRasmussen</dc:creator>
      <dc:date>2024-02-15T17:38:26Z</dc:date>
    </item>
  </channel>
</rss>

