0

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.

Quentin Malgaud
  • 405
  • 6
  • 21
  • possible duplicate of [Delete/Reset all entries in Core Data?](http://stackoverflow.com/questions/1077810/delete-reset-all-entries-in-core-data) – mattt Mar 20 '15 at 00:21
  • 1
    You have shown the code but you haven't explained what isn't working. Have you confirmed that `self.managedObjectContext` is the same as the object context that the app delegate is using? Why do you call the save method in your app delegate instead of simply calling `self.managedObjectContext.save()`? Do you get any errors from the save? - For example, referential constraint violations – Paulw11 Mar 20 '15 at 00:46
  • Not sure if OP wants to reset all of CoreData or just one table. If the latter, this is a duplicate of [this question/answer](http://stackoverflow.com/a/28798544/3985749) which is coded in Swift. – pbasdf Mar 20 '15 at 10:22
  • 1
    possible duplicate of [Whats the easiest way to empty and save a Core Data Table in Swift?](http://stackoverflow.com/questions/28798332/whats-the-easiest-way-to-empty-and-save-a-core-data-table-in-swift) – pbasdf Mar 20 '15 at 10:23
  • It definitely is a duplicate of that post. I am really sorry for that. Thank you for pointing me. @Paulw11: at the for loop, I got a message: '() -> NSFetchedResultsController' does not have a member named 'Generator'. also the appDelegate I referred to was declared in the class earlier: let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate Thank you all for your help, and sorry again for this incomplete post – Quentin Malgaud Mar 20 '15 at 16:08

0 Answers0