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

CoreData and GroupBy, how to get and use results of a .dictionaryReturnType

Forums > SwiftUI

Hey Folks,

I too an trying to select distinct entries from CoreData and am hoping you can help. I believe I am following the essence of @roosterboy's response from here: https://www.hackingwithswift.com/forums/swiftui/dynamically-filtering-atfetchrequest-with-swiftui-distinct-values/13708/13717 but my app will crash when I call for the request to be executed. If I configure my @FetchRequest as below

public class func fetchDistinctCourses(facilityID: Int32) -> NSFetchRequest<Course> {
        let request: NSFetchRequest<Course> = Course.fetchRequest()
        //request.resultType = .dictionaryResultType
        request.predicate = NSPredicate(format: "facility_id == %d", facilityID)
        request.sortDescriptors = []
        request.propertiesToFetch = ["name", "sub_name"]
        request.returnsDistinctResults = true
        request.propertiesToGroupBy = ["name", "sub_name"]
        return request
    }

and the initializer as

init(facility: Facilities) {
        self.facility = facility
        let facilityID = Int32(self.facility.facility_id)
        let request = Course.fetchDistinctCourses(facilityID: facilityID)
        _fetchRequestCoursesByFacilityID = FetchRequest<Course>(fetchRequest: request)        
    }

The app crashes and I get this error (which makes sense as every I've read states .resultType but be a dictionary): Thread 1: "Invalid fetch request: GROUP BY requires NSDictionaryResultType"

When I uncomment the dictionary line I am presented with this error: Thread 1: "NSFetchedResultsController does not support both change tracking and fetch request's with NSDictionaryResultType"

If it helps, at this point in my app, I am only reading from CoreData and I have no desire for the data being being changed except unpon app launch (checking for an updated data set).

Thanks in advance

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.