Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /
SwiftUI

Pass Realm Data Between SwiftUI Views

El SDK de Realm Swift proporciona varias maneras para transferir datos de Realm entre vistas:

  • Pasar objetos de Realm a una vista

  • Use environment injection to:

    • Inyectar un valor de partición en una vista

    • Inyecta un realm abierto en una vista

    • Inject a realm configuration into a view

Cuando utilices el @ObservedRealmObject o el contenedor de propiedades @ObservedResults, se abre implícitamente un dominio y se recuperan los objetos o resultados especificados. Posteriormente, se pueden pasar esos objetos a una vista inferior en la jerarquía.

struct DogsView: View {
@ObservedResults(Dog.self) var dogs
/// The button to be displayed on the top left.
var leadingBarButton: AnyView?
var body: some View {
NavigationView {
VStack {
// The list shows the dogs in the realm.
// The ``@ObservedResults`` above implicitly opens a realm and retrieves
// all the Dog objects. We can then pass those objects to views further down the
// hierarchy.
List {
ForEach(dogs) { dog in
DogRow(dog: dog)
}.onDelete(perform: $dogs.remove)
}.listStyle(GroupedListStyle())
.navigationBarTitle("Dogs", displayMode: .large)
.navigationBarBackButtonHidden(true)
.navigationBarItems(
leading: self.leadingBarButton,
// Edit button on the right to enable rearranging items
trailing: EditButton())
}.padding()
}
}
}

Entorno La inyección es una herramienta útil en el desarrollo de SwiftUI con Realm. Los contenedores de propiedades de Realm ofrecen diferentes maneras de trabajar con valores de entorno al desarrollar su aplicación SwiftUI.

Si utilizas Partition-Based Sync, puedes utilizar la inyección de entorno para pasar el valor de entorno .partitionValue. Inyecte esto en una vista donde realice el @AsyncOpen o el @AutoOpen:

// If there is a logged in user, pass the user ID as the
// partitionValue to the view that opens a realm.
OpenPartitionBasedSyncRealmView().environment(\.partitionValue, user.id)

Luego, cuando utilices el contenedor de propiedades para abrir un realm sincronizado, deja el partitionValue en una string vacía. El contenedor de propiedades rellena el valor desde el objeto de entorno que se ha pasado desde arriba.

// We can use an empty string as the partitionValue here because we're
// injecting the user.id as an environment value from the LoginView.
@AutoOpen(appId: YOUR_APP_SERVICES_APP_ID_HERE, partitionValue: "", timeout: 4000) var autoOpen

Tip

Migrar a la sincronización flexible

You can automatically migrate your App Services Device Sync Mode from Partition-Based Sync to Flexible Sync. This enables you to take advantage of the more expressive and granular Flexible Sync subscriptions and permissions to manage what synced data your users can read and write. For more information, refer to Migrate from Partition-Based Sync to Flexible Sync.

Puedes inyectar un dominio abierto en otra vista de SwiftUI en una vista como valor de entorno. El contenedor de propiedades utiliza este dominio pasado para rellenar la vista:

ListView()
.environment(\.realm, realm)

You can use a realm other than the default realm by passing a different configuration in an environment object.

LocalOnlyContentView()
.environment(\.realmConfiguration, Realm.Configuration( /* ... */ ))

Volver

React to Changes

En esta página