Docs Menu
Docs Home
/ /
Atlas Device SDKs
/ /

Pass Data Between Views - SwiftUI

On this page

  • Pass SDK Objects to a View
  • Pass Environment Values
  • Inject an Opened Database
  • Inject a Database Configuration

Atlas Device SDK provides several ways to pass SDK data between views:

  • Pass SDK objects to a view

  • Use environment injection to:

    • Inject an opened database into a view

    • Inject a database configuration into a view

When you use the @ObservedRealmObject or @ObservedResults property wrapper, you implicitly open a database 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
/// 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()
}
}
}

Environment injection is a useful tool in SwiftUI development with the SDK. Atlas Device SDK property wrappers provide different ways for you to work with environment values when developing your SwiftUI application.

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

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

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

LocalOnlyContentView()
.environment(\.realmConfiguration, Realm.Configuration( /* ... */ ))
← React to Changes