美文网首页
CoreData 增删改查

CoreData 增删改查

作者: uproject | 来源:发表于2016-09-27 12:48 被阅读74次

    CoreData增删改查

    1.1-CoreData增加数据

    Person *p = [NSEntityDescriptioninsertNewObjectForEntityForName:@"Person"inManagedObjectContext:kManagedObjectContext.managedObjectContext];
    p.age= @(13);   
    p.name=@"张三";    
    [kManagedObjectContext save];
    

    1.2-CoreData查询数据

    //1.创建一个查询请求 
     NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Person"]; 
    //2.创建查询谓词(查询条件)
     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@",@"张三"]; 
    //3.给查询请求设置谓词
     request.predicate = predicate; 
    //4.查询数据
     NSArray<Person*> *arr = [kManagedObjectContext.managedObjectContext executeFetchRequest:request error:nil]; 
    
     NSLog(@"%@====%@",arr.firstObject.name,arr.firstObject.age);
    

    1.3-CoreData修改数据

    //1.创建一个查询请求
     NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Person"];
     //2.创建查询谓词(查询条件) 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@",@"张三"]; 
    //3.给查询请求设置谓词 
    request.predicate = predicate; 
    //4.查询数据 
    NSArray<Person*> *arr = [kManagedObjectContext.managedObjectContext executeFetchRequest:request error:nil];
     //5.改变数据 
    arr.firstObject.name = @"李四";
    arr.firstObject.age = @(18); 
    //6.同步到数据库 
    [kManagedObjectContext save];
    

    1.4-CoreData删除数据

    //1.创建一个查询请求
     NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Person"];
     //2.创建查询谓词(查询条件)
     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@",@"张三"];
     //3.给查询请求设置谓词
     request.predicate = predicate; 
    //4.查询数据 
    NSArray<Person*> *arr = [kManagedObjectContext.managedObjectContext executeFetchRequest:request error:nil];
     //5.删除数据 
    [kManagedObjectContext.managedObjectContext deleteObject:arr.firstObject]; 
    //6.同步到数据库
     [kManagedObjectContext save];
    

    1.5-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 增删改查

          本文链接:https://www.haomeiwen.com/subject/vshcyttx.html