user2836375
user2836375

Reputation: 135

RealmSwift: Condition for view not working

I have two different types of Realm Objects with a one-to-many relationship between:

class TopObject: Object, Identifiable {
   @Persisted(primaryKey: true) var id: ObjectId
   @Persisted var name: String
   @Persisted var subObjects: List<SubObject>
}

class SubObject: Object, Identifiable {
   @Persisted(primaryKey: true) var id: ObjectId
   @Persisted var name: String
   @Persisted(originProperty: "subObjects") var topObject: LinkingObjects<TopObject>
}

Inside my view I have a button that should only be visible if the list of subObjects is 0:

@State private var newTopObject = TopObject()

@State private var showModal = false

var body: some View {
    
    Form {
       TextField("Name",text: $newTopObject.name)

       if $newTopObject.subObjects.count == 0 {
          GridRow {
             Button {
                self.showModal = true
             } label: {
                HStack {
                   Image(systemName: "plus.app")
                   Text("Hinzufügen")
                }
                .frame(maxWidth: .infinity)
             }
             .buttonStyle(.bordered)
             .buttonBorderShape(.roundedRectangle(radius: 10))
             .sheet(isPresented: $showModal) {
                NavigationStack {
                   AddSubObject(topObject: $newTopObject)
                }
             }
          }
       }
    }
}

In the view AddSubObject there's the topObject as @Binding var topObject: TopObject.

But after closing this AddSubObject view the button is still visible. If I print the topObject in console (e.g. on the next touch on the button), the list of SubObjects is shown and its length isn't 0 anymore.

What is my problem? Can you help me? Thanks in advance.

Upvotes: 0

Views: 30

Answers (0)

Related Questions