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

SOLVED: Help with CoreData and Entities

Forums > SwiftUI

@Bnerd  

I have the following challenge: I have two Entities in my CoreData: Project & Report The Project has "To Many" with the Report relationship [One Project can have many reports] The Report has "To One" with the Project relationship [One Report belongs only to one Project]

I have created the custom NSManagedObject Subclasses for the Project and the Report and I can succesfully created unique projects and within each Project unique report, so far so good..

The challenge: I want once I navigate to my selected Project to have the list of the reports in Sections by type (type is an attribute of the Report Entity).

I have tried @SectionedFetchRequest which I can make it work with the List Section , but it calls ALL the Reports not only the reports in the project ,

any idea??

P.S. My first View has a list of all Projects, then I navigate to each project and have another list for each report of the project (I am using the array created by the custom NSManaged Object Class)

Thank you!

1      

You need a predicate for your FetchRequest.

https://www.hackingwithswift.com/read/38/7/examples-of-using-nspredicate-to-filter-nsfetchrequest

Depending how your attributes are named it could be something like:

NSPredicate(format: project = %@", project)

1      

You could try using DisclosureGroup, and prepare a new projectList for each report, iterating over the projectList if it is not empty. I am assuming that you are using a viewModel, if not just put the function whereever you you your Project and Report fetch entities. Also you have not mentioned the name of you relationship that links a Project to its Reports, so I will call it hasReport for this example.

In the viewModel

func projectForReport(_ projects: [Project], _ report: String) -> [Project] {
    guard !projects.isEmpty else { return [ ] }

    var newProjectList = [Project]()
    for project in (projects.filter { $0.hasReport == report }  )  // check if report belongs with this project
        {
            newProjectList.insert(project, at: 0)
        }
    return newProjectList  // a list of projects for this report, note could be empty
}

Then


List {
    ForEach(viewModel.reports, id: \.self) { report in

        let projectList = projectForReport(viewModel.projects, report)

        if !projectList.isEmpty {
            DisclosureGroup(report) {        
                ForEach(projectList) { project in
                …
                }
            }
        }
    }
}

1      

@Bnerd  

Thanks for the ideas guys, the NSpredicate on the SectionFetchResults did the trick. In addition since I wanted the view to rebuild once I add a Report, since the SectionFectRequest does not conform to the @ObservableObject I made an additional optional variable of type SectionedFetchResults<String, Report> and used this to update the view once a change was made.

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.