我尝试从CoreData中删除所有“Users”对象,然后通过服务器刷新列表。与Parse中的unpinAllObjectsInBackGroundWithBlock相同的想法。但由于某些原因,我更喜欢使用CoreData而不是Parse的LocalDataStore。 这是我的代码:
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
}
这看起来很简单,却让我发疯。谢谢你的帮助。