A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.mongodb.com/docs/atlas/device-sdks/sdk/swift/swiftui/react-to-changes/ below:

React to Changes - SwiftUI - Atlas Device SDKs

The Swift SDK provides the @ObservedRealmObject property wrapper that invalidates a view when an observed object changes. You can use this property wrapper to create a view that automatically updates itself when the observed object changes.

struct DogDetailView: View {    @ObservedRealmObject var dog: Dog        var body: some View {        VStack {            Text(dog.name)                .font(.title2)            Text("\(dog.name) is a \(dog.breed)")            AsyncImage(url: dog.profileImageUrl) { image in                            image.resizable()                        } placeholder: {                            ProgressView()                        }                        .aspectRatio(contentMode: .fit)                        .frame(width: 150, height: 150)            Text("Favorite toy: \(dog.favoriteToy)")        }    }}

The Swift SDK provides the @ObservedResults property wrapper that lets you observe a collection of query results. You can perform a quick write to an ObservedResults collection, and the view automatically updates itself when the observed query changes. For example, you can remove a dog from an observed list of dogs using onDelete.

Note

The @ObservedResults property wrapper is intended for use in a SwiftUI View. If you want to observe results in a view model, register a change listener.

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()        }    }}

The @ObservedResults property wrapper can take a SortDescriptor parameter to sort the query results.

struct SortedDogsView: View {    @ObservedResults(Dog.self,                     sortDescriptor: SortDescriptor(keyPath: "name",                        ascending: true)) var dogs        var body: some View {        NavigationView {                        List(dogs) { dog in                DogRow(dog: dog)            }        }    }}
Tip

You cannot use a computed property as a SortDescriptor for @ObservedResults.

New in version 10.29.0.

You can observe a results set that is divided into sections by a key generated from a property on the object. We've added a computed variable to the model that we don't persist; we just use this to section the results set.

var firstLetter: String {    guard let char = name.first else {        return ""    }    return String(char)}

Then, we can use the @ObservedSectionedResults property wrapper to observe the results set divided into sections based on the computed variable key.

@ObservedSectionedResults(Dog.self,                          sectionKeyPath: \.firstLetter) var dogs

You might use these observed sectioned results to populate a List view divided by sections:

struct SectionedDogsView: View {    @ObservedSectionedResults(Dog.self,                              sectionKeyPath: \.firstLetter) var dogs        var leadingBarButton: AnyView?    var body: some View {        NavigationView {            VStack {                                List {                    ForEach(dogs) { section in                        Section(header: Text(section.key)) {                            ForEach(section) { dog in                                DogRow(dog: dog)                            }                        }                    }                }                .listStyle(GroupedListStyle())                    .navigationBarTitle("Dogs", displayMode: .large)                    .navigationBarBackButtonHidden(true)                    .navigationBarItems(                        leading: self.leadingBarButton,                                                trailing: EditButton())            }.padding()        }    }}

If your app uses Atlas Device Sync, you can observe the App object to react to login state changes. This enables your app to perform operations while it has an app.currentUser, or direct the user to log in if there is no app.currentUser.

Because Realm caches user credentials on the device, your app can work offline while it has an app.currentUser.

struct FlexibleSyncContentView: View {        @ObservedObject var app: RealmSwift.App    var body: some View {        if let user = app.currentUser {                                                let config = user.flexibleSyncConfiguration(initialSubscriptions: { subs in                let peopleSubscriptionExists = subs.first(named: "people")                let dogSubscriptionExists = subs.first(named: "dogs")                                                if (peopleSubscriptionExists != nil) && (dogSubscriptionExists != nil) {                                        return                } else {                                                                                subs.append(QuerySubscription<Person>(name: "people"))                    subs.append(QuerySubscription<Dog>(name: "dogs"))                }            })            OpenFlexibleSyncRealmView()                .environment(\.realmConfiguration, config)        } else {                        LoginView()        }    }}

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