iOS 数据持久化

本文转自iOS中几种数据持久化方案,仅用作个人记录学习之用。

概论

所谓的持久化,就是将数据保存到硬盘中,使得在应用程序或机器重启后可以继续访问之前保存的数据。在iOS开发中,有很多数据持久化的方案,接下来我将尝试着介绍一下5种方案:

plist文件(属性列表)
preference(偏好设置)
NSKeyedArchiver(归档)
SQLite3
CoreData

沙盒

在介绍各种存储方法之前,有必要说明以下沙盒机制。iOS程序默认情况下只能访问程序自己的目录,这个目录被称为“沙盒”。

1.结构

既然沙盒就是一个文件夹,那就看看里面有什么吧。沙盒的目录结构如下:

"应用程序包"
    Documents
    Library
        Caches
        Preferences
    tmp
2.目录特性

虽然沙盒中有这么多文件夹,但是没有文件夹都不尽相同,都有各自的特性。所以在选择存放目录时,一定要认真选择适合的目录。

  • "应用程序包": 这里面存放的是应用程序的源文件,包括资源文件和可执行文件。

    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSLog(@"%@", path);
    
  • Documents: 最常用的目录,iTunes同步该应用时会同步此文件夹中的内容,适合存储重要数据。

    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    NSLog(@"%@", path);
    
  • Library/Caches: iTunes不会同步此文件夹,适合存储体积大,不需要备份的非重要数据。

    NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
    NSLog(@"%@", path);
    
  • Library/Preferences: iTunes同步该应用时会同步此文件夹中的内容,通常保存应用的设置信息。

  • tmp: iTunes不会同步此文件夹,系统可能在应用没运行时就删除该目录下的文件,所以此目录适合保存应用中的一些临时文件,用完就删除。

    NSString *path = NSTemporaryDirectory();
    NSLog(@"%@", path);
    

Plist文件

plist文件是将某些特定的类,通过XML文件的方式保存在目录中。
可以被序列化的类型只有如下几种:

NSArray;
NSMutableArray;
NSDictionary;
NSMutableDictionary;
NSData;
NSMutableData;
NSString;
NSMutableString;
NSNumber;
NSDate;
1.获得文件路径
NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
NSString *fileName = [path stringByAppendingPathComponent:@"123.plist"];
2.存储
NSArray *array = @[@"123", @"456", @"789"];
[array writeToFile:fileName atomically:YES];
3.读取
NSArray *result = [NSArray arrayWithContentsOfFile:fileName];
NSLog(@"%@", result);
4.注意
只有以上列出的类型才能使用plist文件存储。
存储时使用writeToFile: atomically:方法。 其中atomically表示是否需要先写入一个辅助文件,再把辅助文件拷贝到目标文件地址。这是更安全的写入文件方法,一般都写YES。
读取时使用arrayWithContentsOfFile:方法。

Preference

1.使用方法
//1.获得NSUserDefaults文件
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
//2.向文件中写入内容
[userDefaults setObject:@"AAA" forKey:@"a"];
[userDefaults setBool:YES forKey:@"sex"];
[userDefaults setInteger:21 forKey:@"age"];
//2.1立即同步
[userDefaults synchronize];
    
//3.读取文件
NSString *name = [userDefaults objectForKey:@"a"];
BOOL sex = [userDefaults boolForKey:@"sex"];
NSInteger age = [userDefaults integerForKey:@"age"];
    
NSLog(@"%@, %d, %ld", name, sex, age);
2.注意
  • 偏好设置是专门用来保存应用程序的配置信息的,一般不要在偏好设置中保存其他数据。
  • 如果没有调用synchronize方法,系统会根据I/O情况不定时刻地保存到文件中。所以如果需要立即写入文件的就必须调用synchronize方法。
  • 偏好设置会将所有数据保存到同一个文件中。即preference目录下的一个以此应用包名来命名的plist文件。

NSKeyedArchiver

归档在iOS中是另一种形式的序列化,只要遵循了NSCoding协议的对象都可以通过它实现序列化。由于决大多数支持存储数据的Foundation和Cocoa Touch类都遵循了NSCoding协议,因此,对于大多数类来说,归档相对而言还是比较容易实现的。

1.遵循NSCoding协议

NSCoding协议声明了两个方法,这两个方法都是必须实现的。一个用来说明如何将对象编码到归档中,另一个说明如何进行解档来获取一个新对象。

  • 遵循协议和设置属性
    //1.遵循NSCoding协议 
    @interface Person : NSObject <NSCoding>

    //2.设置属性
    @property (strong, nonatomic) UIImage *avatar;
    @property (copy, nonatomic) NSString *name;
    @property (assign, nonatomic) NSInteger age;

    @end
  • 实现协议方法
    //解档
    - (id)initWithCoder:(NSCoder *)aDecoder {
        if ([super init]) {
            self.avatar = [aDecoder decodeObjectForKey:@"avatar"];
            self.name = [aDecoder decodeObjectForKey:@"name"];
            self.age = [aDecoder decodeIntegerForKey:@"age"];
        }
        
        return self;
    }

    //归档
    - (void)encodeWithCoder:(NSCoder *)aCoder {
        [aCoder encodeObject:self.avatar forKey:@"avatar"];
        [aCoder encodeObject:self.name forKey:@"name"];
        [aCoder encodeInteger:self.age forKey:@"age"];
    }
  • 特别注意
    如果需要归档的类是某个自定义类的子类时,就需要在归档和解档之前先实现父类的归档和解档方法。即 [super encodeWithCoder:aCoder][super initWithCoder:aDecoder] 方法;
2.使用
  • 需要把对象归档是调用NSKeyedArchiver的工厂方法 archiveRootObject: toFile:方法。
    NSString *file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"person.data"];
        
    Person *person = [[Person alloc] init];
    person.avatar = self.avatarView.image;
    person.name = self.nameField.text;
    person.age = [self.ageField.text integerValue];
        
    [NSKeyedArchiver archiveRootObject:person toFile:file];
  • 需要从文件中解档对象就调用NSKeyedUnarchiver的一个工厂方法 unarchiveObjectWithFile:即可。
    NSString *file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"person.data"];
        
    Person *person = [NSKeyedUnarchiver unarchiveObjectWithFile:file];
    if (person) {
       self.avatarView.image = person.avatar;
       self.nameField.text = person.name;
       self.ageField.text = [NSString stringWithFormat:@"%ld", person.age];
    }
3.注意
  • 必须遵循并实现NSCoding协议
  • 保存文件的扩展名可以任意指定
  • 继承时必须先调用父类的归档解档方法

SQLite3

之前的所有存储方法,都是覆盖存储。如果想要增加一条数据就必须把整个文件读出来,然后修改数据后再把整个内容覆盖写入文件。所以它们都不适合存储大量的内容。

1.字段类型

表面上SQLite将数据分为以下几种类型:

integer : 整数
real : 实数(浮点数)
text : 文本字符串
blob : 二进制数据,比如文件,图片之类的

实际上SQLite是无类型的。即不管你在创表时指定的字段类型是什么,存储是依然可以存储任意类型的数据。而且在创表时也可以不指定字段类型。SQLite之所以什么类型就是为了良好的编程规范和方便开发人员交流,所以平时在使用时最好设置正确的字段类型!主键必须设置成integer

2. 准备工作

准备工作就是导入依赖库啦,在iOS中要使用SQLite3,需要添加库文件:libsqlite3.dylib并导入主头文件,这是一个C语言的库,所以直接使用SQLite3还是比较麻烦的。

3.使用
  • 创建数据库并打开

    操作数据库之前必须先指定数据库文件和要操作的表,所以使用SQLite3,首先要打开数据库文件,然后指定或创建一张表。

    /**
     *  打开数据库并创建一个表
     */
    - (void)openDatabase {
        
        //1.设置文件名
        NSString *filename = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"person.db"];
        
        //2.打开数据库文件,如果没有会自动创建一个文件
        NSInteger result = sqlite3_open(filename.UTF8String, &_sqlite3);
        if (result == SQLITE_OK) {
            NSLog(@"打开数据库成功!");
            
            //3.创建一个数据库表
            char *errmsg = NULL;
            
            sqlite3_exec(_sqlite3, "CREATE TABLE IF NOT EXISTS t_person(id integer primary key autoincrement, name text, age integer)", NULL, NULL, &errmsg);
            if (errmsg) {
                NSLog(@"错误:%s", errmsg);
            } else {
                NSLog(@"创表成功!");
            }
                
        } else {
            NSLog(@"打开数据库失败!");
        }
    }
  • 执行指令

    使用 sqlite3_exec() 方法可以执行任何SQL语句,比如创表、更新、插入和删除操作。但是一般不用它执行查询语句,因为它不会返回查询到的数据。

/**
*  往表中插入1000条数据
*/
- (void)insertData {
    
NSString *nameStr;
NSInteger age;
for (NSInteger i = 0; i < 1000; i++) {
   nameStr = [NSString stringWithFormat:@"Bourne-%d", arc4random_uniform(10000)];
   age = arc4random_uniform(80) + 20;
   
   NSString *sql = [NSString stringWithFormat:@"INSERT INTO t_person (name, age) VALUES('%@', '%ld')", nameStr, age];
   
   char *errmsg = NULL;
   sqlite3_exec(_sqlite3, sql.UTF8String, NULL, NULL, &errmsg);
   if (errmsg) {
       NSLog(@"错误:%s", errmsg);
   }
}
    
NSLog(@"插入完毕!");
}
  • 查询指令

    前面说过一般不使用 sqlite3_exec() 方法查询数据。因为查询数据必须要获得查询结果,所以查询相对比较麻烦。示例代码如下:

    sqlite3_prepare_v2() : 检查sql的合法性
    sqlite3_step() : 逐行获取查询结果,不断重复,直到最后一条记录
    sqlite3_coloum_xxx() : 获取对应类型的内容,iCol对应的就是SQL语句中字段的顺序,从0开始。根据实际查询字段的属性,使用sqlite3_column_xxx取得对应的内容即可。
    sqlite3_finalize() : 释放stmt
/**
 *  从表中读取数据到数组中
 */
- (void)readData {
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:1000];
    char *sql = "select name, age from t_person;";
    sqlite3_stmt *stmt;
    
    NSInteger result = sqlite3_prepare_v2(_sqlite3, sql, -1, &stmt, NULL);
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
        
            char *name = (char *)sqlite3_column_text(stmt, 0);
            NSInteger age = sqlite3_column_int(stmt, 1);
            
            //创建对象
            Person *person = [Person personWithName:[NSString stringWithUTF8String:name] Age:age];
            [mArray addObject:person];
        }
        self.dataList = mArray;
    }
    sqlite3_finalize(stmt);
}
4.总结

总得来说,SQLite3的使用还是比较麻烦的,因为都是些c语言的函数,理解起来有些困难。不过在一般开发过程中,使用的都是第三方开源库 FMDB,封装了这些基本的c语言方法,使得我们在使用时更加容易理解,提高开发效率。

FMDB

1.简介

FMDB是iOS平台的SQLite数据库框架,它是以OC的方式封装了SQLite的C语言API,它相对于cocoa自带的C语言框架有如下的优点:

  • 使用起来更加面向对象,省去了很多麻烦、冗余的C语言代码
  • 对比苹果自带的Core Data框架,更加轻量级和灵活
  • 提供了多线程安全的数据库操作方法,有效地防止数据混乱

注:FMDB的gitHub地址

2.核心类

FMDB有三个主要的类:

  • FMDatabase
    一个FMDatabase对象就代表一个单独的SQLite数据库,用来执行SQL语句

  • FMResultSet
    使用FMDatabase执行查询后的结果集

  • FMDatabaseQueue
    用于在多线程中执行多个查询或更新,它是线程安全的

3.打开数据库

和c语言框架一样,FMDB通过指定SQLite数据库文件路径来创建FMDatabase对象,但FMDB更加容易理解,使用起来更容易,使用之前一样需要导入sqlite3.dylib。打开数据库方法如下:

NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"person.db"];

FMDatabase *database = [FMDatabase databaseWithPath:path];    
if (![database open]) {
    NSLog(@"数据库打开失败!");
}  

值得注意的是,Path的值可以传入以下三种情况:

  • 具体文件路径,如果不存在会自动创建

  • 空字符串@"",会在临时目录创建一个空的数据库,当FMDatabase连接关闭时,数据库文件也被删除

  • nil,会创建一个内存中临时数据库,当FMDatabase连接关闭时,数据库会被销毁

4.更新

在FMDB中,除查询以外的所有操作,都称为“更新”, 如:create、drop、insert、update、delete等操作,使用executeUpdate:方法执行更新:

//常用方法有以下3种:   
- (BOOL)executeUpdate:(NSString*)sql, ...
- (BOOL)executeUpdateWithFormat:(NSString*)format, ...
- (BOOL)executeUpdate:(NSString*)sql withArgumentsInArray:(NSArray *)arguments

//示例
[database executeUpdate:@"CREATE TABLE IF NOT EXISTS t_person(id integer primary key autoincrement, name text, age integer)"];   

//或者  
[database executeUpdate:@"INSERT INTO t_person(name, age) VALUES(?, ?)", @"Bourne", [NSNumber numberWithInt:42]];

5.查询
  • 查询方法也有3种,使用起来相当简单:
- (FMResultSet *)executeQuery:(NSString*)sql, ...
- (FMResultSet *)executeQueryWithFormat:(NSString*)format, ...
- (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray *)arguments  

  • 查询示例:
//1.执行查询
FMResultSet *result = [database executeQuery:@"SELECT * FROM t_person"];

//2.遍历结果集
while ([result next]) {
    NSString *name = [result stringForColumn:@"name"];
    int age = [result intForColumn:@"age"];
}

6.线程安全

在多个线程中同时使用一个FMDatabase实例是不明智的。不要让多个线程分享同一个FMDatabase实例,它无法在多个线程中同时使用。 如果在多个线程中同时使用一个FMDatabase实例,会造成数据混乱等问题。所以,请使用 FMDatabaseQueue,它是线程安全的。以下是使用方法:

  • 创建队列。
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:aPath]; 

  • 使用队列
[queue inDatabase:^(FMDatabase *database) {    
          [database executeUpdate:@"INSERT INTO t_person(name, age) VALUES (?, ?)", @"Bourne_1", [NSNumber numberWithInt:1]];    
          [database executeUpdate:@"INSERT INTO t_person(name, age) VALUES (?, ?)", @"Bourne_2", [NSNumber numberWithInt:2]];    
          [database executeUpdate:@"INSERT INTO t_person(name, age) VALUES (?, ?)", @"Bourne_3", [NSNumber numberWithInt:3]];      

          FMResultSet *result = [database executeQuery:@"select * from t_person"];    
         while([result next]) {   

         }    
}];   

  • 而且可以轻松地把简单任务包装到事务里:
[queue inTransaction:^(FMDatabase *database, BOOL *rollback) {    
          [database executeUpdate:@"INSERT INTO t_person(name, age) VALUES (?, ?)", @"Bourne_1", [NSNumber numberWithInt:1]];    
          [database executeUpdate:@"INSERT INTO t_person(name, age) VALUES (?, ?)", @"Bourne_2", [NSNumber numberWithInt:2]];    
          [database executeUpdate:@"INSERT INTO t_person(name, age) VALUES (?, ?)", @"Bourne_3", [NSNumber numberWithInt:3]];      

          FMResultSet *result = [database executeQuery:@"select * from t_person"];    
            while([result next]) {   

            }   

           //回滚
           *rollback = YES;  
    }];     

FMDatabaseQueue 后台会建立系列化的G-C-D队列,并执行你传给G-C-D队列的块。这意味着 你从多线程同时调用调用方法,GDC也会按它接收的块的顺序来执行。

CoreData

1.准备工作
  • 创建数据库

      新建文件,选择CoreData -> DataModel
      添加实体(表),Add Entity
      给表中添加属性,点击Attributes下方的‘+’号
    
  • 创建模型文件

      新建文件,选择CoreData -> NSManaged Object subclass
      根据提示,选择实体
    
  • 通过代码,关联数据库和实体

    - (void)viewDidLoad {
        [super viewDidLoad];
    
      /*
       * 关联的时候,如果本地没有数据库文件,Coreadata自己会创建
       */
      
      // 1. 上下文
      NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
      
      // 2. 上下文关连数据库
    
      // 2.1 model模型文件
      NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
      
      // 2.2 持久化存储调度器
      // 持久化,把数据保存到一个文件,而不是内存
      NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
      
      // 2.3 设置CoreData数据库的名字和路径
      NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
      NSString *sqlitePath = [doc stringByAppendingPathComponent:@"company.sqlite"];
    
      [store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:sqlitePath] options:nil error:nil];
      
      context.persistentStoreCoordinator = store;
      _context = context;
    }
    
2.CoreData的基本操作(CURD)
  • 添加元素 - Create

    -(IBAction)addEmployee{
        // 创建一个员工对象 
        //Employee *emp = [[Employee alloc] init]; 不能用此方法创建
        Employee *emp = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:_context];
        emp.name = @"wangwu";
        emp.height = @1.80;
        emp.birthday = [NSDate date];
      
        // 直接保存数据库
        NSError *error = nil;
        [_context save:&error];
      
        if (error) {
            NSLog(@"%@",error);
        }
    }     
    
  • 读取数据 - Read

    -(IBAction)readEmployee{
        // 1.FetchRequest 获取请求对象
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];
      
        // 2.设置过滤条件
        // 查找zhangsan
        NSPredicate *pre = [NSPredicate predicateWithFormat:@"name = %@",
                          @"zhangsan"];
        request.predicate = pre;
      
        // 3.设置排序
        // 身高的升序排序
        NSSortDescriptor *heigtSort = [NSSortDescriptor sortDescriptorWithKey:@"height" ascending:NO];
        request.sortDescriptors = @[heigtSort];
      
        // 4.执行请求
        NSError *error = nil;
      
        NSArray *emps = [_context executeFetchRequest:request error:&error];
        if (error) {
            NSLog(@"error");
        }
      
        //NSLog(@"%@",emps);
        //遍历员工
        for (Employee *emp in emps) {
            NSLog(@"名字 %@ 身高 %@ 生日 %@",emp.name,emp.height,emp.birthday);
        }
    }
    
  • 修改数据 - Update

    -(IBAction)updateEmployee{
        // 改变zhangsan的身高为2m
        
        // 1.查找到zhangsan
        // 1.1FectchRequest 抓取请求对象
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];
      
        // 1.2设置过滤条件
        // 查找zhangsan
        NSPredicate *pre = [NSPredicate predicateWithFormat:@"name = %@", @"zhangsan"];
        request.predicate = pre;
    
        // 1.3执行请求
        NSArray *emps = [_context executeFetchRequest:request error:nil];
       
        // 2.更新身高
        for (Employee *e in emps) {
            e.height = @2.0;
        }
      
        // 3.保存
        NSError *error = nil;
        [_context save:&error];
      
        if (error) {
            NSLog(@"%@",error);
        }
    }
    
  • 删除数据 - Delete

    -(IBAction)deleteEmployee{
      
        // 删除 lisi
      
        // 1.查找lisi
        // 1.1FectchRequest 抓取请求对象
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];
      
        // 1.2设置过滤条件
        // 查找zhangsan
        NSPredicate *pre = [NSPredicate predicateWithFormat:@"name = %@",
                          @"lisi"];
        request.predicate = pre;
      
        // 1.3执行请求
        NSArray *emps = [_context executeFetchRequest:request error:nil];
      
        // 2.删除
        for (Employee *e in emps) {
            [_context deleteObject:e];
        }
      
        // 3.保存
        NSError *error = nil;
        [_context save:&error];
      
        if (error) {
            NSLog(@"%@",error);
        }
    }
    
3.CoreData的表关联
  • 准备工作
    创建数据库
        新建文件,选择CoreData -> DataModel
        添加实体(表),Add Entity , 注意:这里根据关联添加多个实体
        给表中添加属性,点击Attributes下方的‘+’号

    创建模型文件
        新建文件,选择CoreData -> NSManaged Object subclass
        根据提示,选择实体,注意:这里先选择被关联的实体,最后添加最上层的实体
  • 通过代码,关联数据库和实体
    - (void)viewDidLoad {
        [super viewDidLoad];

        /*
         * 关联的时候,如果本地没有数据库文件,Coreadata自己会创建
         */
        
        // 1. 上下文
        NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
        
        // 2. 上下文关连数据库

        // 2.1 model模型文件
        NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
        
        // 2.2 持久化存储调度器
        // 持久化,把数据保存到一个文件,而不是内存
        NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
        
        // 2.3 设置CoreData数据库的名字和路径
        NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *sqlitePath = [doc stringByAppendingPathComponent:@"company.sqlite"];

        [store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:sqlitePath] options:nil error:nil];
        
        context.persistentStoreCoordinator = store;
        _context = context;

    }
  • 基本操作
    添加元素 - Create

    -(IBAction)addEmployee{

        // 1. 创建两个部门 ios android
        //1.1 iOS部门
        Department *iosDepart = [NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:_context];
        iosDepart.name = @"ios";
        iosDepart.departNo = @"0001";
        iosDepart.createDate = [NSDate date];
        
        //1.2 Android部门
        Department *andrDepart = [NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:_context];
        andrDepart.name = @"android";
        andrDepart.departNo = @"0002";
        andrDepart.createDate = [NSDate date];
        
        //2. 创建两个员工对象 zhangsan属于ios部门 lisi属于android部门
        //2.1 zhangsan
        Employee *zhangsan = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:_context];
        zhangsan.name = @"zhangsan";
        zhangsan.height = @(1.90);
        zhangsan.birthday = [NSDate date];
        zhangsan.depart = iosDepart;
        
        //2.2 lisi
        Employee *lisi = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:_context];
        lisi.name = @"lisi";
        lisi.height = @2.0;
        lisi.birthday = [NSDate date];
        lisi.depart = andrDepart;
        
        //3. 保存数据库
        NSError *error = nil;
        [_context save:&error];
        
        if (error) {
            NSLog(@"%@",error);
        }
    }
  • 读取信息 - Read
    -(IBAction)readEmployee{
        
        // 读取ios部门的员工
        
        // 1.FectchRequest 抓取请求对象
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];
        
        // 2.设置过滤条件
        NSPredicate *pre = [NSPredicate predicateWithFormat:@"depart.name = %@",@"android"];
        request.predicate = pre;
        
          // 4.执行请求
        NSError *error = nil;
        
        NSArray *emps = [_context executeFetchRequest:request error:&error];
        if (error) {
            NSLog(@"error");
        }
        
        //遍历员工
        for (Employee *emp in emps) {
            NSLog(@"名字 %@ 部门 %@",emp.name,emp.depart.name);
        }
    }
  • 其他功能与前几种类似,这里不在赘述
4.CoreData的模糊查询
  • 准备工作和上面类似,主要是查询方式不同
    模糊查询

    -(IBAction)readEmployee{
        // 1.FectchRequest 抓取请求对象
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];

        // 2.设置排序
        // 按照身高的升序排序
        NSSortDescriptor *heigtSort = [NSSortDescriptor sortDescriptorWithKey:@"height" ascending:NO];
        request.sortDescriptors = @[heigtSort];
        
        // 3.模糊查询
        // 3.1 名字以"wang"开头
    //    NSPredicate *pre = [NSPredicate predicateWithFormat:@"name BEGINSWITH %@",@"wangwu1"];
    //    request.predicate = pre;
        
        // 名字以"1"结尾
    //    NSPredicate *pre = [NSPredicate predicateWithFormat:@"name ENDSWITH %@",@"1"];
    //    request.predicate = pre;

        // 名字包含"wu1"
    //    NSPredicate *pre = [NSPredicate predicateWithFormat:@"name CONTAINS %@",@"wu1"];
    //    request.predicate = pre;
        
        // like 匹配
        NSPredicate *pre = [NSPredicate predicateWithFormat:@"name like %@",@"*wu12"];
        request.predicate = pre;

        // 4.执行请求
        NSError *error = nil;
        NSArray *emps = [_context executeFetchRequest:request error:&error];
        if (error) {
            NSLog(@"error");
        }
        
        //遍历员工
        for (Employee *emp in emps) {
            NSLog(@"名字 %@ 身高 %@ 生日 %@",emp.name,emp.height,emp.birthday);
        }
    }
  • 分页查询
    -(void)pageSeacher{
        // 1. FectchRequest 抓取请求对象
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];
        
        // 2. 设置排序
        // 身高的升序排序
        NSSortDescriptor *heigtSort = [NSSortDescriptor sortDescriptorWithKey:@"height" ascending:NO];
        request.sortDescriptors = @[heigtSort];
        
        // 3. 分页查询
        // 总有共有15数据
        // 每次获取6条数据
        // 第一页 0,6
        // 第二页 6,6
        // 第三页 12,6 3条数据
        
        // 3.1 分页的起始索引
        request.fetchOffset = 12;
        
        // 3.2 分页的条数
        request.fetchLimit = 6;
        
        // 4. 执行请求
        NSError *error = nil;
        NSArray *emps = [_context executeFetchRequest:request error:&error];
        if (error) {
            NSLog(@"error");
        }
        
        // 5. 遍历员工
        for (Employee *emp in emps) {
            NSLog(@"名字 %@ 身高 %@ 生日 %@",emp.name,emp.height,emp.birthday);
        }
    }
5.多个数据库的使用
  • 注意:
    创建多个数据库,即创建多个DataModel
    一个数据库对应一个上下文
    需要根据bundle名创建上下文
    添加或读取信息,需要根据不同的上下文,访问不同的实体

  • 关联数据库和实体

    - (void)viewDidLoad {
        [super viewDidLoad];
        
        // 一个数据库对应一个上下文
        _companyContext = [self setupContextWithModelName:@"Company"];
        _weiboContext = [self setupContextWithModelName:@"Weibo"];
    }       

    /**
     *  根据模型文件,返回一个上下文
     */
    -(NSManagedObjectContext *)setupContextWithModelName:(NSString *)modelName{
        
        // 1. 上下文
        NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
        
        // 2. 上下文关连数据库
        // 2.1 model模型文件
        
        // 注意:如果使用下面的方法,如果 bundles为nil 会把bundles里面的所有模型文件的表放在一个数据库
        //NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
        
        // 改为以下的方法获取:
        NSURL *companyURL = [[NSBundle mainBundle] URLForResource:modelName withExtension:@"momd"];
        NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:companyURL];
        
        // 2.2 持久化存储调度器
        // 持久化,把数据保存到一个文件,而不是内存
        NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
        
        // 2.3 告诉Coredata数据库的名字和路径
        NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *sqliteName = [NSString stringWithFormat:@"%@.sqlite",modelName];
        NSString *sqlitePath = [doc stringByAppendingPathComponent:sqliteName];

        [store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:sqlitePath] options:nil error:nil];
        
        context.persistentStoreCoordinator = store;
        
        // 3. 返回上下文
        return context;
    }
  • 添加元素
    -(IBAction)addEmployee{
        // 1. 添加员工
        Employee *emp = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:_companyContext];
        emp.name = @"zhagsan";
        emp.height = @2.3;
        emp.birthday = [NSDate date];
        
        // 直接保存数据库
        [_companyContext save:nil];
        
        // 2. 发微博
        Status *status =[NSEntityDescription insertNewObjectForEntityForName:@"Status" inManagedObjectContext:_weiboContext];
        
        status.text = @"发了一条微博!";
        status.createDate = [NSDate date];
        
        [_weiboContext save:nil];
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,830评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,992评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,875评论 0 331
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,837评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,734评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,091评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,550评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,217评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,368评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,298评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,350评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,027评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,623评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,706评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,940评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,349评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,936评论 2 341

推荐阅读更多精彩内容