删除/重置核心数据中的所有条目?您知道有什么方法可以删除全部存储在Core数据中的条目吗?我的模式应该保持不变;我只想把它重置为空白。编辑我希望通过编程来实现这一点,这样用户就可以在reset纽扣。
3 回答
繁星淼淼
TA贡献1775条经验 获得超11个赞
- (void) deleteAllObjects: (NSString *) entityDescription { NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:entityDescription inManagedObjectContext:_managedObjectContext]; [fetchRequest setEntity:entity]; NSError *error; NSArray *items = [_managedObjectContext executeFetchRequest:fetchRequest error:&error]; [fetchRequest release]; for (NSManagedObject *managedObject in items) { [_managedObjectContext deleteObject:managedObject]; DLog(@"%@ object deleted",entityDescription); } if (![_managedObjectContext save:&error]) { DLog(@"Error deleting %@ - error:%@",entityDescription,error); }}
白猪掌柜的
TA贡献1893条经验 获得超10个赞
更新iOS 10+解决方案
NSBatchDeleteRequest
// create the delete request for the specified entitylet fetchRequest: NSFetchRequest<NSFetchRequestResult> = MyEntity.fetchRequest()let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest) // get reference to the persistent containerlet persistentContainer = (UIApplication.shared.delegate as! AppDelegate).persistentContainer // perform the deletedo { try persistentContainer.viewContext.execute(deleteRequest)} catch let error as NSError { print(error)}
资料来源:
(包括目标C代码) (WWDC 2015视频) (WWDC 2016视频)
- 3 回答
- 0 关注
- 437 浏览
添加回答
举报
0/150
提交
取消