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
Pasa objetos Realm a una vista
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 { (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() } } }
Pass Environment Values
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.
Inject the Partition Value
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. (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.
Inject an Opened Realm
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)
Inject a Realm Configuration
You can use a realm other than the default realm by passing a different configuration in an environment object.
LocalOnlyContentView() .environment(\.realmConfiguration, Realm.Configuration( /* ... */ ))