The @ObservedResults
property wrapper used in the examples on this page is intended for use in a SwiftUI View. If you want to observe results in a view model instead, register a change listener.
New in version 10.19.0.
The Realm Swift SDK allows you to extend .searchable . When you use ObservedResults to query a realm, you can specify collection and keypath in the result set to mark it as searchable.
The collection is the bound collection represented by your ObservedResults query. In this example, it is the dogs
variable that represents the collection of all Dog objects in the realm.
The keypath is the object property that you want to search. In this example, we search the dogs collection by dog name. The Realm Swift .searchable
implementation only supports keypaths with String
types.
struct SearchableDogsView: View { @ObservedResults(Dog.self) var dogs @State private var searchFilter = "" var body: some View { NavigationView { List { ForEach(dogs) { dog in DogRow(dog: dog) } } .searchable(text: $searchFilter, collection: $dogs, keyPath: \.name) { ForEach(dogs) { dogsFiltered in Text(dogsFiltered.name).searchCompletion(dogsFiltered.name) } } } }}
The @ObservedResults property wrapper opens a realm and returns all objects of the specified type. However, you can filter or query @ObservedResults
to use only a subset of the objects in your view.
To filter @ObservedResults
using the NSPredicate Query API, pass an NSPredicate as an argument to filter
:
struct FilterDogsViewNSPredicate: View { @ObservedResults(Dog.self, filter: NSPredicate(format: "weight > 40")) var dogs var body: some View { NavigationView { List { ForEach(dogs) { dog in DogRow(dog: dog) } } } }}
New in version 10.24.0: Use where to perform type-safe queries on ObservedResults.
To use @ObservedResults
with the Realm Type-Safe Query API, pass a query in a closure as an argument to where
:
struct FilterDogsViewTypeSafeQuery: View { @ObservedResults(Dog.self, where: ( { $0.weight > 40 } )) var dogs var body: some View { NavigationView { List { ForEach(dogs) { dog in DogRow(dog: dog) } } } }}
New in version 10.29.0.
The @ObservedSectionedResults property wrapper opens a realm and returns all objects of the specified type, divided into sections by the specified key path. Similar to @ObservedResults
above, you can filter or query @ObservedSectionedResults
to use only a subset of the objects in your view:
@ObservedSectionedResults(Dog.self, sectionKeyPath: \.firstLetter, where: ( { $0.weight > 40 } )) var dogs
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4