The Realm Swift SDK provides several ways to pass realm data between views:
Pass Realm objects to a view
Use environment injection to:
Inject a partition value into a view
Inject an opened realm into a view
Inject a realm configuration into a view
When you use the @ObservedRealmObject
or @ObservedResults
property wrapper, you implicitly open a realm and retrieve the specified objects or results. You can then pass those objects to a view further down the hierarchy.
struct DogsView: View { @ObservedResults(Dog.self) var dogs var leadingBarButton: AnyView? var body: some View { NavigationView { VStack { List { ForEach(dogs) { dog in DogRow(dog: dog) }.onDelete(perform: $dogs.remove) }.listStyle(GroupedListStyle()) .navigationBarTitle("Dogs", displayMode: .large) .navigationBarBackButtonHidden(true) .navigationBarItems( leading: self.leadingBarButton, trailing: EditButton()) }.padding() } }}
Environment injection is a useful tool in SwiftUI development with Realm. Realm property wrappers provide different ways for you to work with environment values when developing your SwiftUI application.
If you are using Partition-Based Sync, you can use environment injection to pass the .partitionValue
environment value. Inject this into a view where you perform the @AsyncOpen
or @AutoOpen
:
OpenPartitionBasedSyncRealmView().environment(\.partitionValue, user.id)
Then, when you use the property wrapper to open a synced realm, leave the partitionValue
an empty string. The property wrapper populates the value from the environment object passed in from above.
@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( ))
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4