Observe in SwiftUI Views
La @ObservedResults El contenedor de propiedades utilizado en los ejemplos de esta página está diseñado para usarse en una vista de SwiftUI. Si prefiere observar los resultados en un modelo de vista, registre un detector de cambios.
Search a Realm Collection
Nueva en la versión 10.19.0.
El SDK de Realm Swift le permite ampliar .buscableCuando utiliza ObservedResults para consultar un reino, puede especificar la colección y la ruta de clave en el conjunto de resultados para marcarlo como buscable.
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 { (Dog.self) var dogs private var searchFilter = "" var body: some View { NavigationView { // The list shows the dogs in the realm. 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) } } } } }
Filtrar o query un Realm con ObservedResults
El contenedor de propiedades @ObservedResults abre un dominio y devuelve todos los objetos del tipo especificado. Sin embargo, puede filtrar o consultar @ObservedResults para usar solo un subconjunto de los objetos de su vista.
Tip
Para obtener más información sobre la sintaxis de consulta y los tipos de consultas que admite Realm, consulte: Leer - Swift SDK y Filtrar datos - Swift SDK.
Filter with an NSPredicate
Para filtrar @ObservedResults utilizando la API de consulta NSPredicate, pase un NSPredicate como argumento filter a:
struct FilterDogsViewNSPredicate: View { (Dog.self, filter: NSPredicate(format: "weight > 40")) var dogs var body: some View { NavigationView { // The list shows the dogs in the realm. List { ForEach(dogs) { dog in DogRow(dog: dog) } } } } }
query con la API de consulta segura por tipo de Realm
New in version 10.24.0: Use where to perform type-safe queries on ObservedResults.
Para utilizar @ObservedResults con la API Realm Type-Safe Query, pasa una consulta en un closure como argumento a where:
struct FilterDogsViewTypeSafeQuery: View { (Dog.self, where: ( { $0.weight > 40 } )) var dogs var body: some View { NavigationView { // The list shows the dogs in the realm. List { ForEach(dogs) { dog in DogRow(dog: dog) } } } } }
Section Filtered Results
New in version 10.29.0.
El contenedor de propiedad @ObservedSectionedResults abre un Realm y devuelve todos los objetos del tipo especificado, divididos en secciones por la ruta de clave especificada. Similar a @ObservedResults anterior, puedes filtrar o query @ObservedSectionedResults para usar solo un subconjunto de los objetos en tu vista:
(Dog.self, sectionKeyPath: \.firstLetter, where: ( { $0.weight > 40 } )) var dogs