С быстрым 2.1 и CoreData я хочу удалить все данные из моей базы данных.IOS - Как очистить базу данных с помощью NSBatchDeleteRequest
с следующим образом, он хорошо работает
let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
let request = NSFetchRequest(entityName: "UserCourse")
let arr = try! context.executeFetchRequest(request)
for item in arr{
context.deleteObject(item as! NSManagedObject)
try! context.save()
}
, но я слышал, что есть лучший способ сделать это с NSBatchDeleteRequest
здесь код
let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
let model = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectModel
let delete = NSBatchDeleteRequest(fetchRequest: NSFetchRequest(entityName: "Data"))
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: model)
try! coordinator.executeRequest(delete, withContext: context)
но он вызывает ошибку.
2015-12-10 01:36:21.195 test[1090:40088] CoreData: error: Illegal attempt to save to a file that was never opened. "This NSPersistentStoreCoordinator has no persistent stores (unknown). It cannot perform a save operation.". No last error recorded.
2015-12-10 01:36:21.202 test[1090:40088] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores (unknown). It cannot perform a save operation.'
Интересно, что должно быть что-то не так с модели или координатор, но я не могу понять, как получить правильный координатор экземпляр, кто-то может мне помочь?