title: SwiftUI Keeping data up to date across views and view models
body: <p>I come from a <code>react-native</code> background and I am having trouble understanding a specific part of MVVM in SwiftUI: having multiple views of the same entities.</p>
  <p>I am building a simple social media app with posts. These posts can be viewed on the <code>Homepage</code>, and on the <code>Profile</code> page of any user.</p>
  <p>This is what the view models look like</p>
  <pre class="lang-swift prettyprint-override"><code>struct HomepagePosts: ObservableObject {
    @Published var posts = [Post]()
  
    ... rest of logic
  }
  
  struct ProfilePosts: ObservableObject {
    @Published var posts = [Post]()
  
    ... rest of logic
  }
  </code></pre>
  <p>My problem is the following. If a user just posted a post, that post will show up on the homepage and on his profile. What if the user decides to update the post? How will the post be updated in all places?</p>
  <p>In React this is done by normalizing state. Instead of keeping separate <code>posts</code> arrays, you keep 2 arrays of <code>postId</code>s, and one dictionary with the <code>postId</code> as key and the entire <code>Post</code> object as the value. That way, updating the post updates it in one place, and every view updates directly.</p>
  <p>How do you do this in SwiftUI?</p>