Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nsfetchedresultscontroller delete cache with name in swift

The title explains it already. What is the swift code for deleting the cache from a nsfetchedresultscontroller.

This is the error i got:

You have illegally mutated the NSFetchedResultsController's fetch request, its predicate, or its sort descriptor without either disabling caching or using +deleteCacheWithName:'

and the code:

if arranged == "naam - A-Z" {
            let sortDescriptor = NSSortDescriptor(key: "name", ascending: true)
            self.fetchedResultsController.fetchRequest.sortDescriptors = [sortDescriptor]
        }else if arranged == "naam - Z-A" {
            let sortDescriptor = NSSortDescriptor(key: "name", ascending: false)
            self.fetchedResultsController.fetchRequest.sortDescriptors = [sortDescriptor]
        }else if arranged == "gemiddelde - 1-10" {
            let sortDescriptor = NSSortDescriptor(key: "gemiddelde", ascending: true)
            self.fetchedResultsController.fetchRequest.sortDescriptors = [sortDescriptor]
        }else if arranged == "gemiddelde - 10-1" {
            let sortDescriptor = NSSortDescriptor(key: "gemiddelde", ascending: false)
            self.fetchedResultsController.fetchRequest.sortDescriptors = [sortDescriptor]
        }

        do {
            try _fetchedResultsController!.performFetch()
        } catch {
            // Replace this implementation with code to handle the error appropriately.
            // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
           // print("Unresolved error \(error), \(error.userInfo)")
            abort()
        }
        tableView.reloadData()

I you need any more information please let me know.

Update:

I want something like this:

fetchedResultsController.deleteCacheWithName("Master")

But i don't know how.

like image 508
Cing Avatar asked Dec 14 '22 08:12

Cing


1 Answers

fetchedResultsController.deleteCacheWithName("Master")

does not compile because

public class func deleteCacheWithName(name: String?)

is a type function, it must be called on the type (class) itself, not on an instance:

// Swift 2:
NSFetchedResultsController.deleteCacheWithName("Master")
// Swift 3:
NSFetchedResultsController<NSFetchRequestResult>.deleteCache(withName: "Master")

If you are using a cache, you must purge the cache before changing any of the fetch request, its predicate, or its sort descriptors. Alternatively, you can create the fetched results controller without using a cache:

NSFetchedResultsController(..., cacheName: nil)
like image 61
Martin R Avatar answered Feb 09 '23 01:02

Martin R