CTPersistance 202

CTPersistance 202

LangLanguage Obj-CObjective C
ReleasedLast Release Feb 2021

Maintained by Casa Taloyum.

Depends on:
SQLCipher>= 0
CTMediator>= 0

  • By
  • CasaTaloyum

pod "CTPersistance"

CTPersistance is a sqlite wrapper which help you to handle with database.

I'm still writing this document, you can check test case for usage.

Async Operation in CTPersistance




CTPersistance provide CTPersistanceAsyncExecutor to do the async staff.


[[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);

see AsyncTestViewController for demo

run CTPersistance project in Simulator, and async test is the live demo for async operation.

Target-Action in CTPersistance

CTPersistance use CTMediator to handle how the database migrate, what secret key to encrypt, where to place the database file.

You should create a target, and just put it into your project, no more code and CTMediator will call your target.

the different database name should have different target, and the target should conform to protocol CTPersistanceConfigurationTarget. see here CTPersistance.h:43

the name of the target object is based on the name of your database file.for example:

say you have a database file which name is:

`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`

Available Actions:

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

return the absolute file path as you want. The database file name will be send in the params.

If CTPersistance can not call this method, CTPersistance will find database in [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:databaseName] folder, if still not found, CTPersistance will create the database file in this path with the database file name.

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

return the class name of migrator manager for database migration.

see Target_MigrationTestDatabase.m:20

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

return secret key to encrypt the database file.

if you have changed the secret key, keep the old key and append new key in a array and return it.

say you encrypt the database in version 1 (it's the version about your app) by the key oldkey, and you changed this key to newkey in version 2. Some days later you changed the key to newkey2 in version3, you should return an array like this:


see Target_TestDatabase.m:13


CTPersistance's record does not have to be a specific object. Any object who conforms to CTPersistanceRecordProtocol can be handled by CTPersistance.

That means you can handle any object like UIViewUIViewController with CTPersistance as long as they conforms to CTPersistanceRecordProtocol. For example you can insert a UIView, and fetch the same data as a dictionary or even UIViewController.

Though CTPersistance does not require your object to inherit from a specific model, CTPersistance provide you CTPersistanceRecord if you do not want to implement the protocol.

CRUD Operations

Create : see here

Read : see here

Update : see here

Delete : see here

Upsert : see here

Other Operations

Database Migration : see here

Index of Columns : see here

Transaction : see here

Change Key of Encryption : see here