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

Core Data View Children For Parent

Forums > SwiftUI

Hi Just getting started with CoreData. I have two entities - Project + Element. These have a one to many relationship. eg each project can have many elements.

I have three views. Project List View - A list of my project with a Navigation Link to Project Detail.

Project Detail View- Shows detail for a chosen project with a list of elements for that project and a navigation link to Add Element View.

Add Element View - Allows you to add a new element and name it.

On the whole my views work. I can add an element and when I navigate back to detail it appears in the list. The issue is that the element list shows all elements for all projects regardless of which project I'm viewing.

List {
                                ForEach(elements, id: \.self) { element in
                                        VStack {
                                            HStack {
                                                Text(element.elementName ?? "Unknown")
                                            }
                                            .font(Font.headline.weight(.bold))
                                        }
                                }// END OF FOR EACH
                                .onDelete(perform: deleteElements)
                            }// END OF LIST

How do I change the list to only show elements forthe current Project??

2      

If you set up a relationship in CoreData between Project and Element then your Project should have a variable called elements. This is usually a (NS)Set. So basically, you need to reference your project in your detail view and use project.elements instead of all elements.

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.