Can I, in some way, use static functions like this one in a SwiftUI view
public func getExercisesAsArray() -> [Exercise] {
let request: NSFetchRequest = NSFetchRequest<Exercise>(entityName: "Exercise")
request.shouldRefreshRefetchedObjects = true
request.includesPropertyValues = true
let context = container.viewContext
do {
let fetchedExercises: [Exercise] = try context.fetch(request) as [Exercise]
return fetchedExercises
} catch let error as NSError {
fatalError("Error fetching Profile for doesProfileExist(): (error), (error.userInfo)")
}
}
Doesn’t seem like I can since it keeps returning nil even though there is entities matching the request in CoreData. In my unit tests functions like this one works fine.
I looked up some simple examples of fetching in SwiftUI but id much rather use the core data functions I have already made to interact with the backend. By having to write these fetch-request up in each view Im also going to have to re-write a lot of boiler plate worsening readability and taking more time.