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

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.

3      

@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?

3      

Thanks for the reply,

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

 @FetchRequest(
      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.

3      

hi,

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,

DMG

3      

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!

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

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.