iOS开源库介绍-FCModel
2015-04-30 本文已影响1403人
Stark_Dylan
直观的说, 这东西就是NSObject的一个扩展,让你方便的在数据库中操作自己的对象,基于FMDB 来做的。今天就来剖析一下这东西的远离。
首先,拿到FCModel的Sources文件,只有6个文件,
![](https://img.haomeiwen.com/i144590/d815e10910ed947b.png)
FCModel是核心的类,在.h文件中引入了FMDB,并做了安全处理
![](http://upload-images.jianshu.io/upload_images/144590-1f4faa42e51bf65d.png)
接着,就是一些静态的字串,为了方便通知的使用
![](http://upload-images.jianshu.io/upload_images/144590-477c67a8bd0ca9e0.png)
然后是一个枚举, 用来标记保存的状态
![](http://upload-images.jianshu.io/upload_images/144590-66eefe56c9006f4f.png)
接下来就是这个开源库的核心地方:
- 属性
![](http://upload-images.jianshu.io/upload_images/144590-db7d8823ec6845ed.png)
-
属性1: 相当于这个对象在数据库中的primaryKey
-
属性2: 所有的字段名字典
-
属性3: 是否有未保存的变化
-
属性4: 是否存在于数据库中
-
属性5: 删了么
-
属性6: 最后的数据库操作过程中的错误
-
一些类方法(这里介绍, 我们直说核心方法)
![](http://upload-images.jianshu.io/upload_images/144590-2132389fb571d62d.png)
![](http://upload-images.jianshu.io/upload_images/144590-6c2d8c6b9baaa20c.png)
![](http://upload-images.jianshu.io/upload_images/144590-62499972b6a7af64.png)
核心类方法的使用步骤:
-
首先
[FCModel closeDatabase];
为了测试一下是否我们之前被打开过、会返回一个bool值,并且会预防一些崩溃的情况。 -
然后,找到数据库的地址
NSString *dbPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"testDB.sqlite3"];
NSLog(@"DB path: %@", dbPath);
在我们需要创建新的数据库之前先删除调之前的数据库
[NSFileManager.defaultManager removeItemAtPath:dbPath error:NULL];
- 打开数据库- 使用类方法
// 打开数据库
[FCModel openDatabaseAtPath:dbPath withSchemaBuilder:^(FMDatabase *db, int *schemaVersion) {
// 基本的一些错误 Log的设置
[db setCrashOnErrors:YES];
db.traceExecution = YES;
[db beginTransaction];
// 下边对db进行操作 并且进行提交
void (^failedAt)(int statement) = ^(int statement){
int lastErrorCode = db.lastErrorCode;
NSString *lastErrorMessage = db.lastErrorMessage;
[db rollback];
NSAssert3(0, @"Migration statement %d failed, code %d: %@", statement, lastErrorCode, lastErrorMessage);
};
// schemaVersion 判断状态
if (*schemaVersion < 1) {
// 创建表
if (! [db executeUpdate:
@"CREATE TABLE Person ("
@" id INTEGER PRIMARY KEY AUTOINCREMENT," // Autoincrement is optional. Just demonstrating that it works.
@" name TEXT NOT NULL DEFAULT '',"
@" colorName TEXT NOT NULL,"
@" taps INTEGER NOT NULL DEFAULT 0,"
@" createdTime INTEGER NOT NULL,"
@" modifiedTime INTEGER NOT NULL"
@");"
]) failedAt(1);
if (! [db executeUpdate:@"CREATE UNIQUE INDEX IF NOT EXISTS name ON Person (name);"]) failedAt(2);
if (! [db executeUpdate:
@"CREATE TABLE Color ("
@" name TEXT NOT NULL PRIMARY KEY,"
@" hex TEXT NOT NULL"
@");"
]) failedAt(3);
// 创建表
*schemaVersion = 1;
}
// 提交
[db commit];
}];
然后 操作一下数据库中的数据
Color *c = [Color instanceWithPrimaryKey:@"colorName"];
c.hex = hex;
[c save];
你得model一定要继承FCModel, 然后直接可以方便的使用对象方法来进行操作。
只需要通过primaryKey拿到当前的对象,然后操作对象的属性之后进行保存即可。
FCModel是一个全局的数据库对象,你可以全局的管理这些对象,也可以通过primaryKey拿到某一个对象进行操作
也可以直接通过类方法进行操作
![](http://upload-images.jianshu.io/upload_images/144590-4a765bcd7c152736.png)
而且你可以获得某个对象当前的状态
![](http://upload-images.jianshu.io/upload_images/144590-ecadf316080c5c19.png)
对象直接操作很简单
![](http://upload-images.jianshu.io/upload_images/144590-492eea2b95d29f8c.png)
推荐大家去看一下。
CopyRight@WildDylan 2015-4-30.