Yes, I posted a new quesiton, but don't know why you couldn't see it, so I tried to ask you again here. Any other method can reach you directly?Please search for this topic: Update SwiftUI View after updating core data value with toggle buttonI can change and update the core data record by clicking the button, but the main problem is the updated record (backend) cannot be reflected immediately in the DetailView, after clicking in some case. However, when I go back to ListView, and click to open DetailView again, it's actually there. Any method to refresh the View, to reflect the updated record instantly?Thanks.
Post
Replies
Boosts
Views
Activity
In SwiftUI, I have a simple core data entity, and one of the attributes called "finished" (Bool).I fetch the core data to create a List in ListView. When I click an item of the List, it goes to the DetailView, which includes the record "finished".In the DetailView, I added a toggle button to SHOW the value of "finished", and meanwhile I use this toggle button to UPDATE the record of "finished" in core data.The curent problem is I can use this toggle button to CHANGE the record in core data; however, the result CANNOT be updated immediately in DeailView. (That means, when I go back to ListView, I see it's updated. And when I click the item and go to DetailView, the record's also properly shown.)1) How to fix this issue? What's the best way to do it?2) What's the proper way to assign the core data value of "finished" to the toggle button, when DetailView's launched?Thanks
Actually in ListView, I've already put the two items you mentioned. In DetailView, and I've also inserted @Environment(\.managedObjectContext) in DetailView.Now the only problem is the record of core data is updated in DetailView, but it can't refresh and show the correct result.I tried the way you said to includ @FetchRequest, but there's some error message.
Thanks for your info, and I've already watched this video. However, this can only help for the List, right? My problem now is on a Detail List View. How to perfectly fix this issue? Anyone can help?
I have the similar problem.I have a simple core data (one of the attributes called "finished" (Bool) ), and I use the data to make a List ("ListView").When click the items on the List, it goes to a Detail List ("DetailView").On the Detail List, I have a button to SHOW the status of "finished", and UPDATE its record in core data. I can use it to change and update the record successfully.When I click the toggle button, at backend, the record's updated.However, the main problem is it cannot reflect the changes in Detail List. Can I REFRESH / RELOAD the Detail List? How to do it? What's the best way to do this task?Many thanks!
func group(_ result : FetchedResults<Songs>)-> [[Songs]] {
return Dictionary(grouping: result) { $0.section! }
.sorted(by: {$0.key < $1.key})
.map {$0.value}
}Based on your codes, I modifed something, and now it works perfectly. Thank you very much!
Got it. Reference to your codes, now the problem's solved. Thank you very much for your time and help!
I tried to apply your function to the Section part of my List:List {
ForEach((group(songs)), id: \.self) { (section: [Songs]) in
Text(section[0].singer!)}
}I got the same result (i.e. Sections go arbitrarily), but I don't know the actual reason. Is there any problem for my codes in the List? I cound't get the result that I want.I'm very new to Swfit. If you can, please give me some explanation for your codes. 🙂I'm really grateful for your time and help!