UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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!

1      

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...

1      

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!

1      

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.

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.