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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值