TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: @FetchRequest not refreshing

Forums > SwiftUI

I have a view in which i call @FetchRequest as below

@FetchRequest(entity: Sales.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Sales.dateToday, ascending: true)]) var mySales: FetchedResults<Sales>

I then call a delete function that delets all sales data in some different view

.alert(isPresented: $deleteData) {
                    Alert(title: Text("Are you sure you want to delete all data?"), primaryButton: .destructive(Text("Delete")) {
                        dataController.shouldReloadData = true  <------------toggle the observed property
                        deleteAll()
                        dataController.save()
                    }, secondaryButton: .cancel())

Now i do have a property in my DataController called

@Published var shouldReloadData = false

which i toggle here as you can see, but the when i go back to the view with

@FetchRequest(entity: Sales.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Sales.dateToday, ascending: true)]) var mySales: FetchedResults<Sales>

it does. not show the data as deleted , it is still there, how can i over come this?

Note - the data is gone once the app is sent to back ground and then opened again. so the data is deleted but the fetchrequest is not refreshed.

2      

Did you do a dataController.viewContext.reset()? Usually that's in a deleteAll method.

3      

@deirdresm thanks , that worked perfectly and now i do not see any values in my view that uses the @FetchRequest, for any one else here , this is my deleteAll() method where i remove an image in documentsDirectory and data from coredata table

func deleteAll() {
        let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Sales")
        let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)

        do {
            let fileURLs = try FileManager.default.contentsOfDirectory(at: documentsURL, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
            let items = try managedObjectContext.fetch(fetchRequest)
            if items.isEmpty {
                emptyList = true
                return
            }
            for files in fileURLs {

                try FileManager.default.removeItem(at: files)
                try managedObjectContext.execute(deleteRequest)
                dataController.container.viewContext.reset()  <--------------where i now use reset as suggested in answer
            }

        } catch {
            print(error)
        }
    }

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

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.