skip to content

In Core Data, a 30,000-sighting list opens slowly and uses a lot of memory, and each region badge fetches every object to count it; how do you fix both?

level: seniorimportance: must knowfreq 60%

answer

  1. the screen shows twenty rows
  2. fetchBatchSize and a proxy array
  3. count without creating objects
  4. count(for:) or countResultType
  5. a default batch size of 0

basics

~20 s

Set fetchBatchSize to about two screens of rows, so Core Data records every matching identity but loads row data in batches through a proxy array, and replace fetch-then-count with context.count(for:), which returns the number without creating any managed objects.

solid answer

~40 s

The default `fetchBatchSize` is 0, meaning unlimited, so every matching object is registered with the context up front. Set to about two screens, say 40, the request is still evaluated in full and every matching identity recorded, but the returned array is a proxy that loads row data one batch at a time as rows are touched; I treat that array like a managed object owned by its context. An `NSFetchedResultsController` uses the same setting from its request. For badges, `try context.count(for: request)` returns the count without creating objects, and it still counts unsaved inserts. `fetchLimit` with `fetchOffset` gives a fixed slice, such as a widget's latest five, and the SQLite store applies the limit itself. Running with `-com.apple.CoreData.SQLDebug 1` shows the difference in the console.

code

swift · 21 lines
swift
import CoreData

@objc(Sighting)
final class Sighting: NSManagedObject {
    @NSManaged var region: String
    @NSManaged var seenAt: Date
}

func listRequest(region: String) -> NSFetchRequest<Sighting> {
    let request = NSFetchRequest<Sighting>(entityName: "Sighting")
    request.predicate = NSPredicate(format: "region == %@", region)
    request.sortDescriptors = [NSSortDescriptor(keyPath: \Sighting.seenAt, ascending: false)]
    request.fetchBatchSize = 40          // about two screens of rows per batch
    return request
}

func badgeCount(region: String, context: NSManagedObjectContext) throws -> Int {
    let request = NSFetchRequest<Sighting>(entityName: "Sighting")
    request.predicate = NSPredicate(format: "region == %@", region)
    return try context.count(for: request)   // no objects are created
}

go deeper

for a junior

Know that fetchLimit caps the results, fetchBatchSize loads them in batches, and count(for:) counts without fetching objects.

for a middle

Explain the proxy array: the full set of matching identities recorded at once, row data loaded per batch on access, and the default of 0 that turns batching off.

for a senior

Diagnose it with SQL logging, size the batch to about two screens, replace counting fetches with count(for:), and keep the proxy array on its context's queue.

for a principal

Set the expectation for every list screen: batch sizes on request builders, count requests for badges, and a performance test against a store of realistic size before release.

## Where the time and memory go A list of 30,000 sightings shows perhaps twenty rows at once. With the default fetch, Core Data executes the request, reads every matching row and registers 30,000 managed objects with the context before the first cell appears. A badge that does `try context.fetch(request).count` pays that whole cost to produce one number. The fix is to ask Core Data for what each screen displays: rows in batches for the list, a count for the badge. ## fetchBatchSize - **Default `0`**, which Apple's documentation describes as infinite: batching is off. - With a **non-zero** batch size, the whole request is still evaluated and the **identities of all matching objects** are recorded, but property data is fetched from the store no more than `fetchBatchSize` objects at a time. - The returned array is a **proxy** that fetches the next batch when an index outside the loaded batches is touched. The documentation calls it an in-memory cursor. - The array's `count` is still the full match count, so a table can size itself correctly. - For thread safety, treat the proxy array as if it were a managed object registered with the context that ran the fetch: use it only on that context's queue. - A good size is about **two screens** of rows; much smaller means more round trips while scrolling. - `NSFetchedResultsController` loads rows with the `fetchBatchSize` of its request, so set it there too. Running a test build with the launch argument `-com.apple.CoreData.SQLDebug 1` prints each SQL statement: the batched fetch first selects only primary keys, then each touched batch loads up to 20 full rows. ## Counting without fetching `try context.count(for: request)` returns how many objects the request would fetch, without creating any of them. It honours the predicate, and its documentation defines the result as the number of objects the request would return from `fetch(_:)`, so matching objects inserted in the context but not yet saved are counted too. Setting `resultType = .countResultType` on an `NSFetchRequest<NSNumber>` and fetching returns the same number wrapped in a one-element array; `count(for:)` is the clearer call. ## Slices and caps 1. **`fetchLimit`** returns at most that many objects. Apple's documentation says the SQL store applies it in the query, while other store types fetch everything and discard the rest. 2. **`fetchOffset`** skips that many matches first; an offset beyond the matches returns an empty array, and offsets are ignored in nested requests such as subqueries. 3. Together they give a fixed slice, such as "the latest five sightings" on a summary card. For a scrolling list, `fetchBatchSize` is usually better than paging by offset, because one fetch with batches already loads only what is shown. ## Where batching stops helping Batching saves work only while code touches a few elements at a time. It quietly stops helping when: - code walks the whole array, for example `results.map(\.species)` or a Swift `sorted(by:)`, which touches every element and so loads every batch; - the batch size is far smaller than a screen, so scrolling triggers a store trip every few rows; - the screen needs a number, not rows, which is a count request's job. The request itself is still evaluated in full: the predicate and sort run over all matching rows in the store, which is why a cheap predicate and a sort the store can serve well still matter. How SQLite plans that query belongs to the database material. ## Choosing the result type | `resultType` | Returns | Use it for | |---|---|---| | `.managedObjectResultType` (default) | managed objects | rows you display or edit | | `.managedObjectIDResultType` | `NSManagedObjectID`s | handing identities to other code | | `.dictionaryResultType` | `NSDictionary` per row | selected columns or aggregates | | `.countResultType` | one `NSNumber` | a count | With object-ID results, the header warns that sort orderings are demoted to best-effort hints when property values are not included in the request, so do not rely on their order then. ## Checking the fix 1. Reproduce with a realistic store of 30,000 rows, not a test fixture of twenty. 2. Turn on `-com.apple.CoreData.SQLDebug 1` and confirm the list's fetch selects keys first and loads batches as you scroll. 3. Confirm each badge issues one `COUNT` statement rather than a full fetch. Two related costs belong to other lessons: faults firing for each row's relationships, and prefetching that cures it, are covered with relationships and faulting; moving a heavy fetch off the main queue is covered with managed object contexts.

  • Does a fetch with fetchBatchSize set return fewer objects than one without it?
    No. The returned array has the full number of matches; batching only changes when each object's data is loaded. That is the difference from `fetchLimit`, which really caps how many objects come back. Use the batch size for a scrolling list and the limit for a fixed slice such as the latest five.
  • Can you pass the batched results array to a background queue for processing?
    No. Apple's documentation says to treat the proxy array as if it were a managed object registered with the context that ran the fetch, so it belongs to that context's queue. Pass the object IDs instead, or run a separate fetch in a context on the other queue.
  • Why might fetchOffset paging be a poor fit for the main sightings list?
    Each page is a separate fetch, and inserts or deletes between pages shift the offsets, so rows can repeat or be skipped. A single fetch with `fetchBatchSize`, or an `NSFetchedResultsController` built on one, already loads only the rows being shown and keeps a stable result set.

A batched fetch is like a library request that hands you the full list of call numbers at once, while the books arrive from the stacks one trolley-load at a time as you reach them; the list is complete, the shelf space you use stays small.

  • fetchBatchSize returns only the first batch, like fetchLimit does.
  • The default fetchBatchSize already loads results in small batches.
  • Counting means fetching the objects and reading the array's count.
  • fetchLimit saves the same disk work on every store type.
  • A batched results array can be handed to any thread like a plain Swift array.