How do apps like Instagram/X keep post state (e.g. likes) synchronized across different feeds in SwiftUI?
I'm trying to understand the correct architecture for shared post state in SwiftUI using the new Observation framework.
I have a simple example where both ContentView and SavedView display PostView.
Observable class Post {
var id: Int
var content: String
var title: String
var isLiked: Bool = false
init(id: Int, content: String, title: String, isLiked: Bool) {
self.id = id
self.content = content
self.title = title
self.isLiked = isLiked
}
func toggleLike() {
isLiked.toggle()
}
}
struct PostView: View {
var post: Post
var body: some View {
HStack {
Text(post.title)
Text(post.content)
Spacer()
Button(post.isLiked ? "Liked" : "Like") {
post.toggleLike()
}
}
}
}
ContentView
struct ContentView: View {
@State private var posts : [Post] = []
var body: some View {
List(posts, id: \.id) { post in
PostView(post: post)
}
.task {
if posts.isEmpty {
let examplePost = Post(
id: 1,
content: "examplecontent",
title: "exampletitle",
isLiked: false
)
posts.append(examplePost)
}
}
}
}
SavedView
struct SavedView: View {
@State private var posts: [Post] = []
var body: some View {
List(posts, id: \.id) { post in
PostView(post: post)
}
.task {
if posts.isEmpty {
let examplePost = Post(
id: 1,
content: "examplecontent",
title: "exampletitle",
isLiked: false
)
posts.append(examplePost)
}
}
}
}
If I like the post in ContentView, the same post in SavedView is still shown as not liked.
I understand that in this example each view creates its own Post instance, so they aren't actually sharing the same object.
My question is more about architecture:
- How is this typically solved in real apps?
- Do apps like Instagram or X (Twitter) maintain a single shared source of truth for every post?
- Is the common approach to have a central store/repository (similar to Redux/TCA) where every screen references the same
Postobject by ID? - Or do they simply refetch the data whenever a user navigates between screens?
- With SwiftUI's new Observation framework, what would be considered the idiomatic architecture?
I'm interested in how large social media apps handle this problem rather than just fixing this sample.