BLACK FRIDAY SALE: Save big on all my Swift books and bundles! >>

SOLVED: Dynamically changing label text from Core Data Entity values

Forums > SwiftUI

I'm trying to update some label texts for my contexMenu depending on the value stored for that object on Core Data. Just before, on the Button definition, I change and save the reference value:

.contextMenu {
                    Button {
                        student.marked.toggle()
                        student.setValue(student.marked, forKey: "marked")
                        do {
                            try moc.save()
                        } catch {
                            print(error.localizedDescription)
                        }
                    } label: {
                        if student.marked {
                            Label("Unmark", systemImage: "bookmark")
                        } else {
                            Label("Mark", systemImage: "bookmark.fill")
                        }
                    }
                }

Data is saved correctly on Core Data, as the rest of the information displayed on view changes in real time. But label text doesn't, until I kill the app and open it again, when both image and text are changed according to the reference value.

I have my Core Data code just at the begining of the view, so everything must update instantly:

@Environment(\.managedObjectContext) var moc
@FetchRequest(sortDescriptors: [SortDescriptor(\.name)]) var students: FetchedResults<Student>

What could I have been doing wrong?

Thanks!

   

Thank you for your response, plenty of pedagogy, great!!

I tried a similar way of dealing with the problem in the line you suggest, with a computed property on my data model. But it didn't worked:

public var marcadoText: String {
        if marcado {
            return "Desmarcar"
        } else {
            return "Marcar"
        }
    }

Now I'm trying the first part of your indications with Label and it throws an error (spanish terms now):


extension Alumno {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<Alumno> {
        return NSFetchRequest<Alumno>(entityName: "Alumno")
    }

    //...

public var marcadoLabel: Label {
        if marcado {
            Label("Desmarcar", image: "bookmark")
        } else {
            Label("Marcar", image: "bookmark.fill")
        }
    }

"Reference to generic type 'Label' requires arguments in <...>" "Label<<#Title: View#>, <#Icon: View#>" "Result of 'Label<Title, Icon>' initializer is unused"

https://dropover.cloud/5b6e39#cd7a415a-798c-4c92-afe4-edeadd276070

Do I need to use another sintax? I'm not sure about it...

   

Good news for this: magically, after changing my View from List to ScrollView, tha automatic change has begun to work. Nothing more to be done: just changing this.

Just for if anyone has the same problem.

Thanks!

   

Thanks for posting your solution!

I think this is a SwiftUI bug because your original code looks good to me.

It would be helpful for you to report the bug using the Feedback Assistant app. (If this app isn’t already installed on your Mac, it will be if you choose Xcode menu Help > Report an Issue.)

From what I've read, Apple has been devoting a lot of effort to fixing SwiftUI bugs.

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.