07-CoreData清除所有数据
CoreData清空数据库
-
清空数据库可以使用删除文件的方式
-
通过沙盒路径进入到沙盒可以看到数据库文件有三个,我们逐一删除便可
-
代码中的kFileName是一个宏 表示创建的数据库文件名
NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; //沙盒中三个文件 NSString *filePath1 = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.db",kFileName]]; NSString *filePath2 = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.db-shm",kFileName]]; NSString *filePath3 = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.db-wal",kFileName]]; NSError *error; BOOL success = [fileManager removeItemAtPath:filePath1 error:&error]; [fileManager removeItemAtPath:filePath2 error:nil]; [fileManager removeItemAtPath:filePath3 error:nil]; if (success) { NSLog(@"Remove fiel:%@ Success!",kFileName); } else { NSLog(@"Could not delete file -:%@ ",[error localizedDescription]); }
CoreData清空数据库实际开发中注意事项
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。- 上一篇: Java数组初始化
- 下一篇: 常用的正则表达式-匹配中英文、字母和数字
copyright © 2008-2019 亿联网络 版权所有 备案号:粤ICP备14031511号-2