I am trying to delete all "Users" objects from CoreData, before refreshing the list via the server. The same idea as unpinAllObjectsInBackGroundWithBlock in Parse. But for some reasons I prefer to use CoreData than the LocalDataStore from Parse. Here is my code:
func deleteAllUsers() {
for users in getFetchedResultController {
self.managedObjectContex.deleteObject(users)
appDelegate.saveContext()
}
}
func getFetchedResultController() -> NSFetchedResultsController{
fetchedResultsController = NSFetchedResultsController(fetchRequest: theFetchRequest(), managedObjectContext: managedObjectContex, sectionNameKeyPath: nil, cacheName: nil)
return fetchedResultsController
}
func theFetchRequest() -> NSFetchRequest {
let fetchRequest = NSFetchRequest(entityName: "UserModel")
let sortDescriptor = NSSortDescriptor(key: "name", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
return fetchRequest
}
This looks pretty simple yet it's driving me crazy. Thank you for your help.