RealmSwift Convert Results to Swift Array
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Realm queries return Results<T>, a lazy, auto-updating collection that reflects live database state. To convert Results<T> to a Swift Array, use Array(results). This creates a snapshot of the data at that point in time, giving you a standard Swift array with full access to methods like map, filter, sorted, and subscript operations. The tradeoff is that arrays are static copies — they do not auto-update when the database changes, and they load all objects into memory immediately rather than using Realm's lazy loading.
Why Convert Results to Array?
Results<T> is lazy and live — it does not load objects until accessed and auto-updates when the Realm changes. This is efficient but comes with restrictions:
Reasons to convert to an Array:
- Use Swift collection methods not available on
Results(e.g.,enumerated(), custom sorting closures) - Pass data to APIs that require
Array - Capture a snapshot that does not change
- Use outside a Realm notification block
Basic Conversion
Filtering and Mapping
Performance: Realm Filter vs Swift Filter
Always apply Realm's .filter() before converting to an Array to minimize the number of objects loaded into memory.
Sorting
Thread-Safe Conversion with freeze()
Realm objects are thread-confined by default. Use freeze() to create thread-safe copies:
Without freeze() (Causes Crash)
Using with SwiftUI
Struct Mapping (Detached from Realm)
For fully detached data, map Realm objects to plain structs:
Common Pitfalls
- Converting large Results to Array unnecessarily:
Array(results)loads every object into memory. For a table with 100,000 rows, this consumes significant memory. Use Realm's lazyResultsdirectly when possible, converting only the subset you need. - Accessing Realm objects on the wrong thread without freeze(): Realm objects are thread-confined. Converting to an
Arraydoes not make the objects thread-safe — the array elements are still Realm objects. Use.freeze()before passing to another thread, or map to plain structs. - Filtering in Swift instead of Realm:
Array(allResults).filter { ... }loads every object into memory before filtering. Useresults.filter("predicate")to filter at the database level first, then convert the smaller result set to an array. - Expecting the array to auto-update: Unlike
Results, a SwiftArrayis a static snapshot. Changes to the Realm database are not reflected in the array. If you need live updates, useResultswith Realm's notification system or SwiftUI's@ObservedResults. - Modifying Realm objects from an array outside a write transaction: Objects in the array are still managed by Realm. Changing a property like
task.title = "new"requires a write transaction (realm.write { task.title = "new" }). Without it, Realm throws an exception.
Summary
- Use
Array(results)to convert RealmResults<T>to a Swift[T]array - Apply Realm's
.filter()and.sorted()before converting to minimize memory usage - Use
.freeze()to create thread-safe copies that can be passed across threads - Map to plain structs with
.map { $0.toDTO() }for fully detached, Codable-ready data - Prefer keeping data as
Results<T>when possible — it is lazy, auto-updating, and memory-efficient
Related reading
- Rearrange an array so that arri becomes arrarri with O1 extra space
- Reasons for using a Bag in Java
- Rebalancing an arbitrary BST?
- Rebalancing rate when new node is added
- Rearranging Tab Bar Controller Order in StoryBoard
- Receive result from DialogFragment
- Receiving kAUGraphErr_CannotDoInCurrentContext when calling AUGraphStart for playback
- Recommendations for using graphs theory in machine learning?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.