测试已测试 | ✓ |
语言语言 | Obj-CObjective C |
许可证 | MIT |
发布最后一个发布 | 2016年12月 |
由 Valerio Santinelli 维护。
依赖 | |
Mantle | ~> 2.0 |
FMDB | ~> 2.3 |
MTLFMDBAdapter 是一个 Mantle 适配器,可以将数据序列化到 FMDB (SQLite) 中,并从 FMDB 中反序列化。这归结为能够通过提供 FMResultSet 创建 MTLModel 实例,反之亦然,创建 INSERT/UPDATE/DELETE 语句以将对象存储在 FMDB 中。
我在许多商业项目中使用过 Core Data 和 RestKit,但从未对结果感到满意。Core Data 很慢,且在处理不同线程时很难正确使用。RestKit 很棒,并且与 RESTful 服务工作得很好,但当你需要与不遵守 REST 协议或以某种方式偏离的服务进行通信时,它就不那么好了。所以这成为了我自己的 Web 服务到对象模型映射的基础。我采用了 Mantle,因为它具有我需要的所有功能,并且其性能远比我最初编写的要好得多,所以把它添加到我的工具箱中是一种很自然的选择。我也喜欢直接处理 SQL 语句,并且 SQLite 一直是我新项目的选择。FMDB 是 SQLite 之上的一个薄代码层,它简化了常见任务,如线程(队列)管理。这个适配器填补了 Mantle 和 FMDB 之间的空白。
欢迎贡献和拉取请求!
以下是一个快速使用的示例
// Grab the Documents folder
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// Sets the database filename
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"MTLFMDBTests.sqlite"];
// Tell FMDB where the database is
db = [FMDatabase databaseWithPath:filePath];
// Open the database
[db open];
// Remove the tables if they're already there. You won't usually do this in real life applications
[db executeUpdate:@"drop table if exists user"];
[db executeUpdate:@"drop table if exists repository"];
// Create the tables we're going to work with
[db executeUpdate:@"create table if not exists user "
"(guid text primary key, name text, age integer)"];
[db executeUpdate:@"create table if not exists repository "
"(guid text primary key, url text)"];
// An empty model we will fill with the record retrieved from the database
MTLFMDBMockUser *resultUser = nil;
// The initial model we will write to the database
MTLFMDBMockUser *user = [[MTLFMDBMockUser alloc] init];
user.guid = @"myuniqueid";
user.name = @"John Doe";
user.age = [NSNumber numberWithInt:42];
// Create the INSERT statement
NSString *stmt = [MTLFMDBAdapter insertStatementForModel:user];
// Get the values of the record in a format we can use with FMDB
NSArray *params = [MTLFMDBAdapter columnValues:user];
// Execute our INSERT
[db executeUpdate:stmt withArgumentsInArray:params];
// Read the record we've just written to the database
NSError *error = nil;
FMResultSet *resultSet = [db executeQuery:@"select * from user"];
if ([resultSet next]) {
resultUser = [MTLFMDBAdapter modelOfClass:MTLFMDBMockUser.class fromFMResultSet:resultSet error:&error];
}
要运行示例项目,请克隆仓库,然后首先从 Example 目录运行 pod install
可以使用 Mantle 的标准方式来定义模型。为了支持 FMDB 序列化,您必须添加 <MTLFMDBSerializing>
协议。
#import "MTLModel.h"
#import <Mantle/Mantle.h>
#import <MTLFMDBAdapter/MTLFMDBAdapter.h>
@interface MTLFMDBMockUser : MTLModel<MTLFMDBSerializing>
@property (nonatomic, copy) NSString *guid;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSNumber *age;
@property (nonatomic, copy) NSSet *repositories;
@end
<MTLFMDBSerializing>
需要在您模型的代码中实现一些方法。
#import "MTLFMDBMockUser.h"
@implementation MTLFMDBMockUser
+ (NSDictionary *)FMDBColumnsByPropertyKey
{
return @{
@"guid": @"guid",
@"name": @"name",
@"age": @"age",
@"repositories": [NSNull null],
};
}
+ (NSArray *)FMDBPrimaryKeys
{
return @[@"guid"];
}
+ (NSString *)FMDBTableName {
return @"user";
}
NSString *stmt = [MTLFMDBAdapter insertStatementForModel:user];
如果 user
是我们上面定义的 MTLFMDBMockUser
类的实例,则结果将是
insert into user (age, guid, name) values (?, ?, ?)
NSArray *params = [MTLFMDBAdapter columnValues:user];
这将返回一个数组,其中包含三个列的值,顺序与语句的列名相同(总是按字母顺序)
MTLFMDBAdapter需要Mantle作为依赖。
Valerio Santinelli, [email protected]
MTLFMDBAdapter根据MIT许可证提供。有关更多信息,请参阅LICENSE文件。