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