<?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: Generic struct 'StateObject' requires that 'GraphicsOverlay' conform to 'ObservableObject' in Swift Maps SDK Questions</title>
    <link>https://community.esri.com/t5/swift-maps-sdk-questions/generic-struct-stateobject-requires-that/m-p/1245442#M16</link>
    <description>&lt;P&gt;That makes sense. &amp;nbsp;I ended up putting it into global view model object so I can add graphics from anywhere in my app. &amp;nbsp;However, the sample threw me off for a bit.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the clarification.&lt;/P&gt;</description>
    <pubDate>Wed, 04 Jan 2023 21:06:24 GMT</pubDate>
    <dc:creator>DuanePfeiffer</dc:creator>
    <dc:date>2023-01-04T21:06:24Z</dc:date>
    <item>
      <title>Generic struct 'StateObject' requires that 'GraphicsOverlay' conform to 'ObservableObject'</title>
      <link>https://community.esri.com/t5/swift-maps-sdk-questions/generic-struct-stateobject-requires-that/m-p/1245285#M14</link>
      <description>&lt;P&gt;When I add the following line to my view, &amp;nbsp;I get the error from compiler. &amp;nbsp;Project won't compile now. &amp;nbsp;I got this line of code from the 'Search with geocode' sample.&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;@StateObject&lt;/SPAN&gt; &lt;SPAN&gt;&lt;STRONG&gt;private&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN&gt;&lt;STRONG&gt;var&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN&gt;inspectedOverlay&lt;/SPAN&gt; = GraphicsOverlay()&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Generic struct 'StateObject' requires that 'GraphicsOverlay' conform to 'ObservableObject'&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jan 2023 15:23:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/swift-maps-sdk-questions/generic-struct-stateobject-requires-that/m-p/1245285#M14</guid>
      <dc:creator>DuanePfeiffer</dc:creator>
      <dc:date>2023-01-04T15:23:36Z</dc:date>
    </item>
    <item>
      <title>Re: Generic struct 'StateObject' requires that 'GraphicsOverlay' conform to 'ObservableObject'</title>
      <link>https://community.esri.com/t5/swift-maps-sdk-questions/generic-struct-stateobject-requires-that/m-p/1245369#M15</link>
      <description>&lt;P&gt;It is recommended to put these reference type objects in a viewmodel-like object that conforms to &lt;STRONG&gt;ObservableObject&amp;nbsp;&lt;/STRONG&gt;protocol like below. See an Apple example &lt;A href="https://developer.apple.com/tutorials/sample-apps/imagegallery#Data-Model-Gathering-Image-URLs" target="_self"&gt;here&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="swift"&gt;class Model: ObservableObject {
    let graphicsOverlay = GraphicsOverlay()
    // ... other properties below
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The reasoning behind is that a graphics overlay isn't "&lt;SPAN&gt;a type of object with a publisher that emits before the object has changed. (from &lt;A href="https://developer.apple.com/documentation/combine/observableobject" target="_self"&gt;Apple doc&lt;/A&gt;)&lt;/SPAN&gt;". In other words, it doesn't drive an UI update with published states, thus is semantically wrong to be an&amp;nbsp;ObservableObject.&lt;/P&gt;&lt;P&gt;However, as of today there isn't a property wrapper in SwiftUI that means "I want to keep an object unchanged between view re-computation, but the object doesn't effectively need to be observable." StateObject kind of works, but is not perfect. Moreover, Apple may moved away from ObservableObject&lt;SPAN&gt;&amp;nbsp;in a future release (&lt;/SPAN&gt;&lt;A href="https://forums.swift.org/t/pitch-observation/62051" target="_blank" rel="nofollow noopener"&gt;pitch&lt;/A&gt;&lt;SPAN&gt;).&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;With that said, you can still conform the&amp;nbsp;&lt;STRONG&gt;GraphicsOverlay&lt;/STRONG&gt; type to ObservableObject&amp;nbsp;protocol yourself, such as&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="swift"&gt;// somewhere in your code...
extension GraphicsOverlay: ObservableObject {}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;then you can hold the object as an&amp;nbsp;&lt;STRONG&gt;@StateObject&amp;nbsp;&lt;/STRONG&gt;without issue.&lt;/P&gt;&lt;P&gt;Finally, the reason that the sample viewer doesn't get this error but your code does, is that in the&amp;nbsp;&lt;SPAN&gt;&lt;EM&gt;Search with geocode&lt;/EM&gt; sample, it imported the toolkit, which in 200.0.0-beta added a few ObservableObject conformances like above. We are considering removing this conformance in 200.1.0.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jan 2023 18:04:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/swift-maps-sdk-questions/generic-struct-stateobject-requires-that/m-p/1245369#M15</guid>
      <dc:creator>Ting</dc:creator>
      <dc:date>2023-01-04T18:04:47Z</dc:date>
    </item>
    <item>
      <title>Re: Generic struct 'StateObject' requires that 'GraphicsOverlay' conform to 'ObservableObject'</title>
      <link>https://community.esri.com/t5/swift-maps-sdk-questions/generic-struct-stateobject-requires-that/m-p/1245442#M16</link>
      <description>&lt;P&gt;That makes sense. &amp;nbsp;I ended up putting it into global view model object so I can add graphics from anywhere in my app. &amp;nbsp;However, the sample threw me off for a bit.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the clarification.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jan 2023 21:06:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/swift-maps-sdk-questions/generic-struct-stateobject-requires-that/m-p/1245442#M16</guid>
      <dc:creator>DuanePfeiffer</dc:creator>
      <dc:date>2023-01-04T21:06:24Z</dc:date>
    </item>
  </channel>
</rss>

