WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

RESOLVED - @FetchRequest Function

Forums > SwiftUI

Good evening,

I have an app that is using core data. The data is run through a bunch of functions to break the data down into different arrays of stuff (such as taking the time stamps and breaking it down by day of the week, or hour of the day).

What I am trying to figure out is how do you make a function run when the the change on the @FetchRequest is observed. User enters data, data is saved to core data, fetch request observes it, but then I am stuck. I can force a reload by reloading the page, and having the function run on appear, but that doesnt seem to be the a very good solution.


@FetchRequest will automatically reload the view

For example: https://www.hackingwithswift.com/quick-start/swiftui/how-to-create-a-core-data-fetch-request-using-fetchrequest

language List will be reload whenever coredata of <ProgrammingLanguage> changes

all data change under language struct will refresh the view too.

you can also add <Entity>.objectWillChange.send() when you update coredata.

If it is not view but only data, the data fetched from [Entity].NSFetchRequest<Entity> will be updated automatically too.

or what do you want to reload?


Thanks for the reply,

So how I have it now (and feel free to tell me I did this crazy 😄), is i have:

      entity: Entries.entity(),
      sortDescriptors: [
          NSSortDescriptor(keyPath: \entries.type, ascending: true),
          NSSortDescriptor(keyPath: entries.date, ascending: false),
  var allData: FetchedResults<Entries>

And what it does is it throw the fetched data into a a bunch of functions for calculating data

data.setAllData(data: allData)

And the data file has a bunch of published information used throughout the app.

So the Fetch Request is being observed, and all the published variables are being observed, but throwing the fetched data to the published ones isn't.



i'd recommend looking at 2 options.

(1) instead of using a @FetchRequest in a View and then handing that data off to a data object (a class that is an ObservableObject), consider using an NSFetchedResultsController (FRC) in the data object instead. when you create the FRC, and whenever the FRC kicks in response to a change in Core Data, use what comes back to reset all the @Published properties of the data object (and any view that tracks the data object as an @ObservedObject will get the update message).

(2) instead of having a data object that's more-or-less global, keep any necessary @FetchRequest in a View, and implement a local function that reformats/reorganizes the data for that view.

so, for example, if your @FetchRequest tracks Entries, but your view only shows a list of specific data about (a subset of) those entries (using various filter, map, or dictionary methods), then write your List as

List {
  ForEach( myFunction(allData) ) { entry in
    // ...

where myFunction does whatever data transformations are necessary to produce an array or sequence of Identifiable data to drive the list.

hope that helps,



hey! thanks for the help! after reading your suggestions, option 2 seems to be a winner. I think i was getting too fancy with my data, and recalcuating all of it each time coredata was changed does seems overkill. Im gonna try changing it up and see how it goes.

Once again, thanks!


Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

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.