牛骨文教育服务平台(让学习变的简单)
博文笔记

07-CoreData清除所有数据

创建时间:2016-07-11 投稿人: 浏览次数:167


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清空数据库实际开发中注意事项


    声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。