Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
Click here >
Docs Menu
Docs Home
/ /
SwiftUI

Filter Data - SwiftUI

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.

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 {
@ObservedResults(Dog.self) var dogs
@State 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)
}
}
}
}
}

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

For more information about the query syntax and types of queries that Realm supports, see: Read - Swift SDK and Filter Data - Swift SDK.

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 {
// The list shows the dogs in the realm.
List {
ForEach(dogs) { dog in
DogRow(dog: dog)
}
}
}
}
}

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 {
@ObservedResults(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)
}
}
}
}
}

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:

@ObservedSectionedResults(Dog.self,
sectionKeyPath: \.firstLetter,
where: ( { $0.weight > 40 } )) var dogs

Volver

Escritura de datos

En esta página