|
I use an split view on my Mac app that shows a list of students (left) and a detailedView of each of them (right). When I delete one student from left (detailed) view, it doesn't dissapear from both detailedView nor general list until I load another view and come back again.So changes are saved to CoreData correctly, but not updated in memory. I have found some information about "merging" in-memory data and CoreData-updated data, but always refered to batch deletion, not just for one item. My delete function is defined as easy as:
How could I add some lines of code in order to merge/update displayed data? |
|
Your student Entity that is loaded in your views should be @Observedobject, I don't have split views, but whenever I save my CoreData for Entitities that are marked @Observedobject the views update. |
|
Thanks @Bnerd. It should, but it does not. And I use @ObservedObject on all of them. In fact, it is not a split view, but a Navigation View. When deleting student on iOS target, I get this "animation": https://www.youtube.com/shorts/YJbkZckfdMs But on MacOS, same code, doesn't update any of both views: neither detailed one nor list one: https://www.youtube.com/watch?v=KUfJT21EYTc&feature=youtu.be Any further ideas? |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure and A/B test your entire paywall UI without any code changes or app updates. Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
If you can share the code for your list maybe we will get some ideas. In the IOS the View updates probably because you move between list to detail, but on the MACOS it seems that the element remains although it's information becomes nil. |
|
The relevant code on each view is as follows. From studentsView (general list, parent):
From studentCellView (not so relevant, only the refrence for the list):
From studentDetailView:
From studentEditView:
On CoreData model:
|
|
|
|
filteredStudents is an array to contain fetched content from CoreData for the view:
As soon as the view appears, it fills. I use the same function to update data based on searchings:
I'm almost sure that it is related to the way in-memory data merges with persistent data, as explained here: https://stackoverflow.com/questions/60230251/swiftui-list-does-not-update-automatically-after-deleting-all-core-data-entity But all information I find is related to bulk deletion, and NSBatchDeleteRequests, and this is just deleting one unique object. |
|
try appending to |
|
I was trying that this afternoon. But I can't find how, as
is defined on StudentsView (parent list view), and a parameter "order" (an String defining students sorting) is requiered. I don't have that data on studentEditView, so how can I add a call to this function? I tried with a public "external" function too, but I still need the parameters... |
|
Then pass
You may have to re-think where the @State vars are defined to be sure you can pass them down to child views that reference them. |
|
Try to rethink your structure. Everytime I run into such a stopping point I do that. F.e. you could use a class DataController as EnvironmentObject which does all the deleting and fetching for you. You don't have to put that all in your Student class. |
|
Ok, as I expected, your "filteredStudents" is the @State property that controls your view, but does not understand if the Entity Student has updates i.e. You deleted a student. If you want to keep the current code try this : In your StudentsView add this
In the child views add a Binding
Place in your relevant functions:
Finally at your StudentsView add this below your .onAppear
You should be ok now ;) |
|
Thanks you all very much.
I've tried even with @AppStorage vars in order to use an app-wide variable to rely on to force updating, but it doesn't change its value on "studentsView" when I change it on "studentEditView". I don't know why. Trying the other options suggested by @Hatsushira and @bobstern, although them forces to rebuild a great part of the app :-( The most frustrating thing is that my app has been working perfectly well on iOS, but having three NavigationView-relationed views visible on MacOS at the same time makes it inoperative for such a tiny reason. I tried as well not making use of an intermediate "filteredStudents" array, because I know it is the origin of the problem, as @Bnerd suggest, but I can modify sorting and reordering directly from fectRequestData, as doing so:
throws error: "Cannot assign to property: 'students' is a get-only property". Doing this way, both studentsView and studentDetailView, connected, updates like a charm. So... I reformulate my problem: how could I re-write checkData method in order to operate directly with this fetchedData? |
|
Do you have an Observable model that you could pass it along as an EnvironmentObject? If yes, put the boolean there as an @Published var If not add also the binding var to your StudentDetailView. It should work, the Edit updates the binding var, this updates the binding in the StudentDetailView, which should update the @State at your first view. |
|
Apart from xcdatamodel, I have a Student+CoreDataClass...
...a Student+CoreDataProperties...
... and a Student+CoreDataMethods.
I think none of them mark Student as @ObservableObject, doesn't it? If os, you suggest passing it to enviornment on .app file this way?
|
|
I am not referring to the CoreData entities, myself when I face similar scenarios, I use my separate small "model" file to share information between views. "you suggest passing it to enviornment on .app file this way?" -> Yes. But give it a try first with the Bidings as described in my previous response first, I believe it should work. |
|
I have finally found the way!! Paul himself has had de key there all the time: just an init() and an underscore_ for the FetchRequest for dinamically filtering. https://www.hackingwithswift.com/books/ios-swiftui/dynamically-filtering-fetchrequest-with-swiftui Now I build my lists directly from fetchedResults -no intermediate array at all- so deletions display real-time on all displayed views :-))) Thank your all very much for your suggestions, and Paul for this "treasure". |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure and A/B test your entire paywall UI without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.
Link copied to your pasteboard.