BLACK FRIDAY SALE: Save big on all my Swift books and bundles! >>

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: 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


Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

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.