Post

Replies

Boosts

Views

Activity

SwiftUI ForEach on Core Data children list
Hi all, I'm trying to figure out a proper way to list and remove/add items in a Core Data relationship. Image that a Project entity has a one-to-many relationship to Task (one-to-one inverse). When listing projects it works smoothly: ForEach(self.projects) { project in 	 // NavigationLink to a ProjectDetailsView } So, when it goes to another View to show the selected project details, then the Core Data relationship get very weird. What's the correct way to do a ForEach on selectedProject.tasks since it's an old NSSet? not compatible with any SwiftUI component? How to add/remove items on this relationship? I tried some workaround that I found, like to extract the tasks to an array, it works fine for listing on ForEach, but when editing the items, it needs to leave the DetailsView to "reload" the updated tasks and then re-extract them again on a Array listing the correct items then. selectedProject.addToTasks(newlyCreateTask) try? self.managedObjectContext.save() It saves the Core Data entities, but do not update automatically the wrapped Array of tasks, what do not happens for project list because ForEach is watching the Core Data entities directly. I didn't find any WWDC video with this kind of example, on YouTube is full of solutions to list only, doing the Array wrapping but none for add/remove items. Cheers.
3
0
5.3k
Oct ’20