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

Pass Realm Data Between SwiftUI Views

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

  • Pass Realm objects to a view

  • Use environment injection to:

    • Inject a partition value into a view

    • 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

Migrate to Flexible Sync

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.

You can inject a realm that you opened in another SwiftUI view into a view as an environment value. The property wrapper uses this passed-in realm to populate the view:

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