← All postsBlog

Taming runaway Combine publishers: killing redundant Core Data fetches

An engineer testing an app on an iPhone next to his laptop

The problem

The app worked fine in the demo. It worked fine on my device. Then a user with a real library — 500+ locally stored video recordings — opened the recordings screen, and the app became unusable: scrolling stuttered, taps lagged by seconds, and creating or editing a single recording froze the UI.

Nothing in the logs pointed at a culprit. No crash, no obvious hot loop in our code, no runaway animation. The app was just busy — constantly, invisibly busy — and only past a certain amount of data.

That "only past a certain amount of data" detail is the whole story. A bug that scales with your dataset is almost never the code you are looking at. It is the code that runs per item when you thought it ran once.

Our approach

We stopped guessing and opened Instruments with the Core Data template, then just used the app: create a recording, rename one, delete one. The trace was damning.

On any single mutation — one insert, one edit — we were re-fetching the entire recordings table. Rename one recording, and we pulled every recording back out of the store. The Core Data instrument showed it as a real fetch every time: save a recording or create a new one, and every local recording was fetched again. A routine flow multiplied it: creating a new recording fired one full fetch, giving it a name fired another, and the follow-up status update fired a third. Three whole-table fetches to add one item.

Under normal use — a background upload finishing, a status flipping, the user tapping around — these arrived in bursts, so we were firing full-table fetches up to ten times per second. Some of that work ran on the main thread, unisolated, so the fetches were not merely wasteful; they were blocking the UI directly. At 20 objects that is invisible. At 500+ it is a wall.

The mechanism was a FetchedResultsPublisher, a tidy little Combine wrapper around NSFetchedResultsController:

class FetchedResultsPublisher<Result: NSFetchRequestResult>: Publisher {
    typealias Output = [Result]
    typealias Failure = Never
    // ...
    // On every controllerDidChangeContent(_:), emit the FULL array again.
}

NSFetchedResultsController watches the context and calls controllerDidChangeContent on any change it detects. Our publisher's response was to emit the whole result set. No diffing, no debounce, no throttle — one context change in, one full array out.

That alone would have been survivable. What made it toxic was what sat downstream in the recordings manager. The local stream was merged with the remote stream, and on every emission the entire list was re-mapped and re-combined:

Publishers.CombineLatest(localRecordingsPublisher, remoteRecordingsPublisher)
    .map { local, remote in
        // rebuild, de-dupe, and re-map EVERY recording, every time
    }

So a single edit produced one context change → one full Core Data fetch of N objects → one full re-map of N objects → one full merge with the remote list. Multiply by roughly ten per second. The publisher was not reacting to what changed. It was reacting to the fact that something changed, and paying the full price each time.

There was a second, uglier problem underneath: the fetched NSManagedObjects were being passed across threads into the mapping code. NSManagedObject is not thread-safe, so that was a latent crash waiting for a race. Fixing it was necessary plumbing rather than the interesting part, so we will leave it at this: managed objects should never leave the queue of the context that owns them.

The hard decisions and trade-offs

The obvious fix is to reach for .debounce or .removeDuplicates and call it a day. We did not, because that only hides the cost. You still fetch the whole table, you just do it slightly less often. The problem was not the frequency of the fetches. It was that the fetch was the wrong unit of work.

So we made a bigger, riskier change: we stopped letting Core Data be the thing the UI subscribes to.

The shift is easiest to see side by side — the same single change, before and after:

Before: every single change makes the fetched-results publisher re-emit the full array, fetch all N objects and re-map them, up to ~10× per second; after: one fetch at login into an actor-backed snapshot store, then each change mutates one object and updates one snapshot
Before: every single change makes the fetched-results publisher re-emit the full array, fetch all N objects and re-map them, up to ~10× per second; after: one fetch at login into an actor-backed snapshot store, then each change mutates one object and updates one snapshot

Core Data became a persistence detail behind two new pieces.

Immutable snapshots. Every NSManagedObject is converted, the moment it is fetched, into a Sendable value type:

public struct LocalVideoSnapshot: VideoSnapshot, Sendable, Equatable {
    public let objectURL: URL   // URI ref, so we can re-fetch the real object to mutate it
    public let id: String
    // ...all fields captured by value
}

Because it is Sendable and Equatable, a snapshot is safe to hand across any queue or async boundary, and cheap to compare — which kills the redundant-work problem at the root.

An actor as the single source of truth. All snapshots live in one in-memory store, and every mutation routes through it:

public actor LocalVideoSnapshotsStore {
    private var items: [LocalVideoSnapshot]? = nil {
        didSet {
            let list = items ?? []
            reindex(list)              // O(n) id→index map
            snapshotSubject.send(list) // one broadcast point
        }
    }
    private var byId: [String: Int] = [:]

    func insert(_ item: LocalVideoSnapshot)      // add one
    func update(_ item: LocalVideoSnapshot)      // change one, skip if unchanged
    func batchUpdate(_ updates: [LocalVideoSnapshot])
    func delete(id: String)                      // remove one
}

The UI subscribes to the store's publisher, not to Core Data. Core Data work is funnelled onto one background context, and every fetch or update block converts to a snapshot before returning, so the managed object never escapes.

We still publish the whole array on every change, and that is deliberate. It lives in memory, no database read sits behind it, and every subscriber gets one simple contract. The diffing happens where it costs nothing: each row is Identifiable, so SwiftUI's ForEach compares by id and redraws only the rows that changed. And update returns early when the new snapshot equals the old one, so a change that changes nothing never reaches the UI.

The trade-off is worth stating plainly: we gave up NSFetchedResultsController's "it just updates itself" behaviour. Now we own change propagation, and every insert, update and delete has to call the right store method explicitly. That is more code and more discipline — forget to notify the store and the UI goes stale. We accepted that cost because explicit, targeted updates are exactly what the old design lacked. A duplicated in-memory layer you keep in sync by hand is a price; paying it bought the ability to touch one item instead of all of them.

Here is the honest part, though. Core Data was not the villain. We were using it backwards.

NSFetchedResultsController is built to drive a UITableView or UICollectionView efficiently. It hands you per-object insert, update, move and delete diffs precisely so you can animate just the rows that changed. The original code took that granular, batteries-included change stream and flattened it into a single "something changed, here is the whole array again" signal. It wanted the reactive feel without using the machinery that makes reactivity cheap, and that mismatch is what nearly buried the app.

In another design, the right fix might have been to lean harder into the controller's diffs and feed them into a diffable data source. We went the snapshot-and-store route because our list was not a single homogeneous fetch — it was a local stream merged with a remote one — and we needed one place that owned that merged truth.

The outcome

The behaviour inverted. The full table is now fetched exactly once, right after login. From that point on nothing re-reads the whole store: every action goes through the store, which publishes the updated list once, straight from memory.

Renaming a recording changes exactly one entry in the store, publishes once, and SwiftUI redraws one row. The create-name-update flow that used to fire three full-table fetches now emits three single-item changes. Zero full-table fetches after the first load, where before there were up to ten per second, some of them blocking the main thread.

The pathological case — a burst of background upload-status changes hammering the list — went from re-fetching and re-mapping the entire table on every tick to updating only the rows involved.

At 500+ recordings the screen scrolls and responds the same as it did at 20, because the database work no longer scales with the size of the library. It scales with what actually changed.

The whole lifecycle now reads as one path: fetch once at startup, then every mutation and every read goes through the store. The database is touched to persist a change, never to read the list back.

Stack & takeaways

Stack: Core Data, Combine, Swift Concurrency (actor), Instruments (Core Data template).

  • Do not flatten a granular change stream. NSFetchedResultsController is not the problem — it hands you per-object diffs on purpose. Wrapping it to re-emit the entire result set on every change throws away exactly the information that makes it cheap, and quietly turns every small write into O(N) work several times a second. Use the tool the way it is built, or do not reach for it.
  • .debounce treats the symptom. If the unit of work is wrong, doing it less often just delays the pain. Change what runs, not only how often.
  • Snapshots make concurrency boring, in a good way. Converting NSManagedObjects to Sendable value types at the fetch boundary removes an entire class of cross-thread bugs and makes Equatable-based change detection trivial.
  • Give the UI one source of truth that it owns. An actor that holds state and broadcasts every change from one place is easier to reason about than a database-driven stream you do not control — at the cost of propagating changes yourself, which is a cost worth paying.
  • A bug that scales with your data is telling you where the per-item work is hiding. Follow the "only at N objects" clue. It is almost always the answer.