Skip to main content

Posts

Showing posts with the label SwiftUI

SwiftUI View Not Updating? Why You Should Stop Using @ObservedObject for Defaults

  You have built a standard MVVM (Model-View-ViewModel) feature in SwiftUI. Your logic is sound, your logic inside the   ObservableObject   prints the correct values to the console, but the UI refuses to reflect those changes. Or perhaps even worse: the UI updates for a split second, then snaps back to its initial state, erasing user input. This behavior is rarely a bug in your business logic. It is almost always a misunderstanding of the SwiftUI View Lifecycle and memory ownership. If you are initializing a ViewModel directly inside a View using  @ObservedObject var model = MyViewModel() , you are inadvertently telling SwiftUI to destroy and recreate your data every time the screen redraws. Here is the technical breakdown of why this happens and the architectural pattern you must use to fix it. The Problem: Ephemeral Views vs. Persistent State To understand the bug, you must accept a core tenet of SwiftUI:  Views are value types (Structs), not reference types. ...

Optimizing SwiftUI `Table` Performance for 50,000+ Row Data Grids on macOS

  You have built a native macOS utility using SwiftUI. It looks modern, the code is clean, and it handles test data beautifully. Then you load a real-world dataset—50,000 rows of log entries or financial records—and the application falls apart. Scrolling stutters, the CPU spikes to 100%, and sorting the columns freezes the UI for several seconds. While the older AppKit  NSTableView  handles millions of rows effortlessly using cell recycling and delegation, SwiftUI’s declarative nature introduces a "Diffing Tax" that can cripple performance if not managed correctly. This guide details exactly why SwiftUI  Table  chokes on large datasets and provides a production-ready architectural pattern to render 50k+ rows at 60 FPS. The Root Cause: The SwiftUI Diffing Tax To fix the lag, you must understand what SwiftUI is doing during a render pass. When you pass an array of 50,000 items to a SwiftUI  Table , the framework must determine what changed since the last fram...