IOS数据持久化之Core Data(二) - 单表操作


上篇我们简要介绍了Core Data的框架,我们对Core Data有了一个基本的认识,这些都是理论上的,下面我们做一个Demo,执行一些简单的增、删、改、查等操作。


首先我们新建一个Project,在创建的时候勾选"Use Core Data"复选框,


创建完成以后如下:

系统自动为我们创建了一个与工程同名的Data Model,打开该Model,新建一个Entity,设置Entity的Name,添加Entity的Attributes,如下图:



由于我们在创建Project的时候勾选了"Use Core Data",所以系统已经自动为我们生成了Core Date增、删、改、查的对象,首先我们看看系统自动创建好的Core Data对象,对象说明看注释,截图如下:

下面我们需要做的就是完成对数据的增、删、改、查,首先我们新建一个Student类,字段参照我们创建的Student Entity中的Attributes,代码如下:

#import <Foundation/Foundation.h>

@interface Student : NSObject

@property(nonatomic,strong)NSString *no;
@property(nonatomic,strong)NSString *studentName;
@property(nonatomic,retain)NSNumber *age;
@property(nonatomic,retain)NSNumber *sex;

@end

#import "Student.h"

@implementation Student

@synthesize  age;
@synthesize no;
@synthesize studentName;
@synthesize sex;

@end

然后我们在界面上添加四个方法,分别是数据的添加、修改、删除、查询,代码如下:

添加数据:

#pragma mark 保存数据
-(BOOL)SaveData:(Student *)stu{
    BOOL result = YES;
    NSManagedObjectContext *context =[self managedObjectContext];
    Student *student =[NSEntityDescription insertNewObjectForEntityForName:TableName inManagedObjectContext:context];

    student.no =stu.no;
    student.age = stu.age;
    student.studentName = stu.studentName;
    student.sex = stu.sex;
    
    NSError *error = nil;
    if([context save:&error]){
        result = YES;
        NSLog(@"保存数据成功");
    }else{
        result = NO;
        NSLog(@"保存数据失败");
    }
    
    return result;
}
修改数据:

-(BOOL)UpdateData:(NSString *)no StudentName:(NSString *)newName{
    BOOL result = YES;
    
    NSManagedObjectContext *context =[self managedObjectContext];
    
    NSPredicate *predicate =[NSPredicate predicateWithFormat:@" no like[cd] '123'"];
    
    NSFetchRequest *fetchRequest =[[NSFetchRequest alloc] init];
    NSEntityDescription *entity =[NSEntityDescription entityForName:TableName inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    [fetchRequest setPredicate:predicate];
    
    NSError *error = nil;
    NSArray *resultArr =[context executeFetchRequest:fetchRequest error:&error];
    for(Student *stu in resultArr){
        stu.studentName = newName;
    }
    if([context save:&error]){
        result = YES;
        NSLog(@"更新成功");
        
    }else{
        result = NO;
        NSLog(@"更新失败");
    }
    return result;
}

删除数据:

-(BOOL)DeleteData:(NSString *)no{
    BOOL result =  YES;
    
    NSManagedObjectContext *context =[self managedObjectContext];
    NSEntityDescription *entity =[NSEntityDescription entityForName:TableName inManagedObjectContext:context];
    NSPredicate *predicate =[NSPredicate predicateWithFormat:@"no like[cd] '%@'",no];
    NSFetchRequest *fetchRequest =[[NSFetchRequest alloc] init];
    //[fetchRequest setIncludesPropertyValues:NO];
    
    [fetchRequest setEntity:entity];
    [fetchRequest setPredicate:predicate];
    
    NSError *error = nil;
    
    NSArray *array =[context executeFetchRequest:fetchRequest error:&error];
    if(!error && array && [array count])
    {
        for(NSManagedObject *obj in array){
            [context deleteObject:obj];
        }
        if([context save:&error]){
            result = YES;
        }else{
            result = NO;
        }
    }
    return result;
}

查询:

-(NSArray *)SelectData:(int)pagesize RecordOffset:(int)currentPage{
    NSManagedObjectContext *context =[self managedObjectContext];
    NSFetchRequest *fetchRequest =[[NSFetchRequest alloc] init];
    [fetchRequest setFetchLimit:pagesize];
    [fetchRequest setFetchOffset:currentPage];
    
    NSEntityDescription *entity =[NSEntityDescription entityForName:TableName inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    NSError *error = nil;
    NSArray *array =[context executeFetchRequest:fetchRequest error:&error];
    return array;
}

示例代码下载


参考:

1、https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/Articles/cdTechnologyOverview.html#//apple_ref/doc/uid/TP40009296-SW1

2、https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Predicates/Articles/pCreating.html



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS中的MMKV是一个高性能、轻量级的键值存储库,可以用于持久化数据。它由微信团队开发,通过使用C++编写的底层存储引擎,提供了比NSUserDefaults更快速和可靠的存储解决方案。 要在iOS中使用MMKV进行持久化,你可以按照以下步骤进行操作: 1. 集成MMKV库:首先,需要将MMKV库添加到你的iOS项目中。你可以通过CocoaPods或手动导入方式添加依赖。具体的集成步骤可以参考MMKV的官方文档。 2. 创建MMKV实例:在使用MMKV之前,你需要创建一个MMKV实例来操作数据。可以使用下面的代码创建一个MMKV实例: ``` // 导入MMKV头文件 #import <MMKV/MMKV.h> // 创建MMKV实例 NSString *mmkvID = @"your_mmkv_id"; MMKV *mmkv = [MMKV mmkvWithID:mmkvID]; ``` 在创建MMKV实例时,需要指定一个唯一的ID来区分不同的实例。这个ID会被用作数据存储的文件名。 3. 存储数据:使用MMKV实例可以方便地存储各种类型的数据。例如,存储字符串可以使用以下代码: ``` NSString *key = @"your_key"; NSString *value = @"your_value"; [mmkv setString:value forKey:key]; ``` 除了字符串外,MMKV还支持存储其他基本数据类型(如整数、布尔值等),以及NSData对象。 4. 读取数据:使用MMKV实例可以快速读取存储的数据。以下是一个读取字符串数据的示例: ``` NSString *key = @"your_key"; NSString *value = [mmkv getStringForKey:key]; ``` 同样地,你也可以使用适当的方法来读取其他类型的数据。 通过这些简单的步骤,你可以在iOS应用中使用MMKV库进行持久化操作。希望这能对你有所帮助!如果有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值