CTPersistance 202

CTPersistance 202

测试已测试
Lang语言 Obj-CObjective C
许可 NOASSERTION
发布上次发布2021年2月

Casa Taloyum 维护。



 
依赖
SQLCipher>= 0
CTMediator>= 0
 

  • CasaTaloyum

pod "CTPersistance"

CTPersistance 是一个 sqlite 包装器,帮助您处理数据库。

我还在编写这份文档,您可以查看使用情况的测试用例。

CTPersistance 中的异步操作

不要直接使用 GCD

不要直接使用 GCD

不要直接使用 GCD

CTPersistance 提供了 CTPersistanceAsyncExecutor 来处理异步任务。

读取

[[CTPersistanceAsyncExecutor sharedInstance] read:^{
    NSInteger count = COUNT;
    while (count --> 0) {
        TestRecord *record = (TestRecord *)[self.testTable findWithPrimaryKey:@(count) error:NULL];
        NSLog(@"%@", record.primaryKey);
    }
}];

写入

[[CTPersistanceAsyncExecutor sharedInstance] write:^{
    NSInteger count = COUNT;
    while (count --> 0) {
        NSNumber *primaryKey = [self.testTable insertValue:@"casa" forKey:@"name" error:NULL];
        NSLog(@"%@", primaryKey);
    }
}];

查看 AsyncTestViewController 的示例代码

在模拟器中运行 CTPersistance 项目的示例,其中 async test 是异步操作的实时演示。

CTPersistance 中的 Target-Action

CTPersistance 使用 CTMediator 来处理数据库迁移,加密密钥以及数据库文件放置的位置。

您应该创建一个目标,并将其放入项目中,无需更多代码,CTMediator 将调用您的目标。

不同的数据库名称应具有不同的目标,并且目标应遵循协议 CTPersistanceConfigurationTarget查看 CTPersistance.h:43

目标对象的名称基于您的数据库文件名称。例如

假设您有一个名为


`aaa.sqlite`, and the target should be `Target_aaa`

`aaa_bbb.sqlite`, and the target should be `Target_aaa`

`aaa_bbb`, and the target should be `Target_aaa`

`aaa.abc.sqlite`, and the target should be `Target_aaa`

`aaa`, and the target should be `Target_aaa`

可用操作

  • - (NSString *)Action_filePath:(NSDictionary *)params

返回所需的全路径。数据库文件名将包含在 params 中。

如果 CTPersistance 无法调用此方法,则 CTPersistance 会在 [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:databaseName] 文件夹中寻找数据库,如果还未找到,CTPersistance 则会在该路径中创建以数据库文件名命名的数据库文件。

  • - (CTPersistanceMigrator *)Action_fetchMigrator:(NSDictionary *)params

返回数据库迁移管理器的类名。

参见 Target_MigrationTestDatabase.m:20

  • - (NSArray *)Action_secretKey:(NSDictionary *)params

返回用于加密数据库文件的密钥。

如果您更改了密钥,请保留旧密钥并在数组中追加新密钥并返回。

例如,您在版本 1(涉及您的应用程序的版本)中通过密钥 oldkey 加密数据库,并在版本 2 中将其更改为 newkey。几天后,您在版本 3 中将其更改为 newkey2,您应该返回一个如下所示的数组

@[
    @"oldkey",
    @"newkey",
    @"newkey2",
]

参见 Target_TestDatabase.m:13

记录

CTPersistance 的记录不必是特定对象。任何满足 CTPersistanceRecordProtocol 的对象都可以由 CTPersistance 处理。

这意味着您可以使用 CTPersistance 处理任何对象,例如 UIViewUIViewController,只要它们遵循 CTPersistanceRecordProtocol。例如,您可以将 UIView 插入并获取与字典相同的数据,甚至 UIViewController

尽管 CTPersistance 不要求您的对象从特定模型继承,但 CTPersistance 提供了 CTPersistanceRecord,如果您不想实现该协议。

CRUD 操作

Create : 参见此处

Read : 参见此处

Update : 参见此处

Delete : 参见此处

Upsert : 参见此处

其它操作

数据库迁移 : 参见此处

列索引 : 参见此处

事务 : 参见此处

更改加密密钥 : 参见此处