My subject is that once I edit a template, the UI in BeginWorkoutView doesn’t replace accurately. I consider I’m by some means misusing @ObservableObject/@Revealed/@Mainactor, however can not for the lifetime of me determine how. Each tutorial appears to make use of the sample I’m at the moment utilizing, and it really works for them.
I’ve a major web page, BeginWorkoutView. From right here, you possibly can create new exercise or create exercise templates. You can too see all created templates, and click on to edit them.
here’s a simplified model of that:
struct BeginWorkoutView: View {
@ObservedObject var templateManager = TemplateManager.shared
var physique: some View {
NavigationView{
ScrollView{
Part("Begin from scratch"){
HStack{
NavigationLink("New Exercise"){
//new exercise web page
}
NavigationLink("New Template"){
CreateTemplateView( viewModel: CreateTemplateViewModel(), editMode: true)
}
}
}
Part("Begin from template") {
ForEach(templateManager.templates, id: .id.self) { template in
TemplateRowView(template: template)
}
}
}
}
}
}
I handle my knowledge in a template supervisor. Im in a position so as to add new exercises and so they present up on my BeginWorkoutView web page no subject. As I mentioned, once I edit the title, it doesn’t replace in BeginWorkoutView. From what I can inform, the templateManager record of templates IS being up to date, but it surely doesnt appear to set off the UI. I’m at the moment utilizing the @mainactor/@Observable object construction as a result of i wish to help pre-IOS 17.
@MainActor
remaining class TemplateManager: ObservableObject {
@Revealed personal(set) var templates: [WorkoutTemplate] = []
static let shared = TemplateManager()
init(){
getTemplates()
}
func getTemplates() {
//will get templates from firebase
}
func updateTemplates(template: WorkoutTemplate) {
Job{
do {
//updates templates in firebase
//HERE is the place the UI ought to be getting the sign to replace. Clearly spot changing the template didnt work, however changing the whole factor wont work both.
var up to date = templates
if let index = up to date.firstIndex(the place: { $0.id == template.id }) {
up to date[index] = template
} else {
up to date.append(template)
}
self.templates = up to date
} catch {
print("unable to replace templates")
}
}
}
}
whether or not i’m creating a brand new template or enhancing one, i exploit createtemplateview/createtemplateviewmodel. listed below are simplified variations with principally simply the operate calls. I dont consider the problem is right here however im including them in case they’re useful.
@MainActor
remaining class CreateTemplateViewModel: ObservableObject {
@Revealed var template: WorkoutTemplate
var templateManager = TemplateManager.shared
init(template: WorkoutTemplate) {
self.template = template
}
init(){
self.template = WorkoutTemplate(identify: "Exercise Template", workoutModules: [])
}
func addTemplateList() {
templateManager.updateTemplates(template: template)
}
func removeTemplateList() {
templateManager.removeTemplateList(templateId: template.id)
}
}
struct CreateTemplateView: View {
@StateObject var viewModel: CreateTemplateViewModel
var physique: some View {
ScrollView{
StringTextField(preview: "Template Title", $viewModel.template.identify )
.padding()
//exhibits editable modules for the template
Button("save"){
viewModel.addTemplateList()
}
}
}
}
there are "hacks" on the market like utilizing notifications or mix to inform the UI to replace, however I consider this could work if executed accurately. I’m principally scripting this to study these knowledge buildings and i am having a troublesome time greedy this one so I would positively prefer to unravel it slightly than slap a bandaid repair on it.