How do I bind to a value in an @Observable view model without invalidating all views in SwiftUI?

Migrating your view models from the @ObservedObject protocol to the @Observable macro should reduce the amount of ceremony in your view models (e.g., @Published property wrappers) while increasing the performance of your app as the @Observable macro promises to more intelligent only invalidate the views that need an update. However, when I bind to a string value in an @Observable view model, the opposite seems to happen:

  1. Binding directly to the string value in the ViewModel through $viewModel.text causes all views to be invalided, even the ones that do not depend on the text value.
  2. Trying to create a @Bindable reference to the viewModel as per the docs gives a compiler error.
  3. Introducing subviews that allow for a @Bindable view model work (in the sense that now only the views that depend on the text value get invalidated), but does not seem like a sustainable approach as we now have a lot of ceremony again – be it in the form of unnecessary subviews rather than e.g., @Published property wrappers.

Below is a simple app demonstrating the issues. When running this on a simulator the let _ = Self._printChanges() calls will show which views get invalided when in the logs.

I’m using Xcode 15.4 and targeting iOS 17+.


import SwiftUI

// MARK: - ContentView
struct ContentView: View {
    var body: some View {
        let _ = Self._printChanges()
        NavigationStack {
            UserListView()
                .navigationTitle("View Invalidations...")
        }
    }
}

// MARK: - Model

@Observable
class User: Identifiable {
    var id = UUID()
    
}

// MARK: - Mock
extension User {
    static func random() -> User {
        return .init()
    }
    static func randomLot() -> [User] {
        var result: [User] = .init()
        for _ in 1..<50 {
            result.append(.random())
        }
        return result
    }
}


// MARK: - UserListView
struct UserListView: View {
    // ✅ Use @State because this view is the source of truth for this view model.
    @State var viewModel = ViewModel()
    // ❌ Direclty initialize a Bindable -> all views in list still get invalidated when using textfield.
//    @Bindable private var viewModel: ViewModel = .init()
    var body: some View {
        let _ = Self._printChanges()
        List {
            ForEach(viewModel.items) {
                UserRowView(item: $0)
            }
        }
        // ❌ Bind directly to property of viewmodel -> all views in list get invalidated when using textfield.
        .overlay(alignment: .bottom) {
            SomeCustomTextView(text: $viewModel.text)
        }
        // ❌ create @Bindable in view -> compiler error
//        .overlay(alignment: .bottom) {
//            VStack {
//                @Bindable var model = viewModel
//                SomeCustomTextView(text: $model.text)
//
//            }
//        }
        // ✅ create @bindable in another view
//        .overlay(alignment: .bottom) {
//            SomeOverLayView(viewModel: viewModel)
//        }

    }
}

// MARK: UserListView - ViewModel

extension UserListView {
    @Observable class ViewModel {
        var items: [User] = User.randomLot()
        var text: String = ""
        
    }
}

// MARK: - SomeCustomTextView

struct SomeCustomTextView: View {
    @Binding var text: String
    var body: some View {
        let _ = Self._printChanges()
        TextField("Can we type without invalidating all rows?", text: $text)
            .padding()
            .background(.regularMaterial, in: RoundedRectangle(cornerRadius: 25.0))
            .padding()
            .shadow(radius: 20)
    }
}

// MARK: - UserRowView

struct UserRowView: View {
    @State private var viewModel: ViewModel
    init(item: User) {
        _viewModel = .init(wrappedValue: .init(item: item))
    }
    var body: some View {
        let _ = Self._printChanges()
        Text(viewModel.user.id.uuidString)
            .frame(maxWidth: .infinity, alignment: .leading)
        
    }
}
// MARK: UserRowView - ViewModel

extension UserRowView {
    @Observable class ViewModel {
        var user: User
        init(item: User) {
            self.user = item
        }
   
    }
}


// 🔍 MARK: - Alternative, but not ideal solutions

// ✅ If we create a @Bindable of the viewmodel, we can type text without invalidating the rows...
// FIXME:  But this approach woudld introduce a lot of overhead in production applications.
struct SomeOverLayView: View {
    var viewModel: UserListView.ViewModel
    var body: some View {
        let _ = Self._printChanges()
        @Bindable var model = viewModel
        SomeCustomTextView(text: $model.text)

    }
}

// ✅ Another approach would be to let the view take the viewmodel as a dependency.
// FIXME: but this again introduces a lot of overhead.
struct SomeCustomTextViewV2: View {
    @Bindable var viewModel: UserListView.ViewModel
    var body: some View {
        let _ = Self._printChanges()
        TextField("test", text: $viewModel.text)
    }
}


#Preview {
    ContentView()
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật