DBAccess 1.6.13

DBAccess 1.6.13

测试已测试
语言语言 Obj-CObjective C
许可 MIT
发布最后发布2016年6月

JoeAdrian Herridge维护。



DBAccess 1.6.13

  • Adrian Herridge

DBAccess iOS ORM

[DBAccess]是一个功能齐全且可免费使用的iOS ORM。

在保持现有托管对象的同时替换CoreData,但删除了谓词和冗长的语法。

改用简单且干净的面向对象语法,以及快速简洁的内部查询。

DBAccess甚至还有将现有CoreData表迁移过来 conversions方法。

它定期更新,并在许多公共应用程序中始终受重视,从其他开发人员的反馈中汲取灵感,并支持作者通过StackOverflow或直接通过电子邮件。

它的座右铭很简单,要快速、易于实现,并成为任何开发者的首选。

入门

将[DBAccess]集成到您的项目中再简单不过了。本指南应该可以满足您入门所需的所有内容,并让您思考,在没有它的情况下如何开发了iOS应用程序。

已经尽了最大的努力确保您能够尽快开始工作,这包括尽可能支持多种数据类型、与现有类协同工作,以及集成框架所需的最小配置。

要求

DBAccess版本 最小iOS目标 注释
1.x.x iOS 7 需要Xcode 5。

要安装它,只需将以下行添加到您的Podfile中

pod "DBAccess"

用法

设置项目

一旦将DBAccess框架添加到您的应用程序中,您就需要尽快在应用程序生命周期中启动它。DBDelegate也需要设置,我们建议将其添加到您的应用程序代理中。

// Objective-C
@interface AppDelegate : UIResponder <UIApplicationDelegate, DBDelegate>
// Swift
class AppDelegate: UIResponder, UIApplicationDelegate, DBDelegate

然后您需要尽早启动DBAccess

// Objective-C
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [DBAccess setDelegate:self];
    [DBAccess openDatabaseNamed:@"myDatabase"];
    return YES;
}
// Swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    DBAccess.setDelegate(self)
    DBAccess.setPersistSynthesizedProperties(true)
    DBAccess.openDatabaseNamed("myDatabase")

    return true
}

创建数据对象

DBAccess对象是带有属性定义的常规类,ORM会检查所有这些类,并在SQLite数据库中映射它们的结构。如果您添加或删除列,则表将更新以表示当前类的结构。

在Objective-C中,属性需要在代码中使用@dynamic实现,这是为了让ORM控制从数据库中检索和设置这些值的操作,在Swift中,属性被定义为var dynamic

示例对象

Objective-C

//  Header File : Person.h
#import <DBAccess/DBAccess.h>

@interface Person : DBObject
@property NSString*         name;
@property int               age;
@property int               payrollNumber;
@end

// Source File : Person.m
#import "Person.h"

@implementation Person
@dynamic name,age,payrollNumber;
@end

Swift

@objc(Person)
class Person: DBObject {
    dynamic var name : String!
    dynamic var age : NSNumber!
    dynamic var payrollNumber : NSNumber!
}

创建对象、设置值与持久化

Objective-C

// Create a new object
Person* thisPerson = [Person new];

// Set some properties
thisPerson.age = 37;
thisPerson.payrollNumber = 123456;
thisPerson.name = @"Adrian Herridge";

// Persist the object into the datastore
[thisPerson commit];

Swift

// Create a new object
var thisPerson = Person()

// Set some properties
thisPerson.age = 37;
thisPerson.payrollNumber = 123456;
thisPerson.name = "Adrian Herridge";

// Persist the object into the datastore
thisPerson.commit()

查询对象

为了检索对象,我们使用与每个DBObject类关联的DBQuery对象。这可以接受可选参数,如.where、.limit、.orderBy和.offset。所有的参数都会返回同一个查询对象,允许在单个嵌套指令中构建查询。

对查询对象的最终调用使用fetch、count、sum、fetchLightweight和fetchAsync完成,这将执行查询并返回结果。

取出整个表格

Objective-C

DBResultSet* results = [[Person query] fetch];

Swift

var results : DBResultSet = Person.query().fetch()

带参数的查询示例

Objective-C

DBResultSet* results = [[[[[Person query]
                       where:@"age = 35"]
                       limit:99]
                     orderBy:@"name"]
                             fetch];

Swift

var results : DBResultSet = Person.query().whereWithFormat("age = %@", withParameters: [35]).limit(99).orderBy("name").fetch()

删除对象

Objective-C

for (Person* person in [[Person query] fetch]) {
    [person remove];
}

// or the shorthand is to use the removeAll method on the DBResultSet object
[[[Person query] fetch] removeAll];

Swift

for person in Person.query().fetch() {
    person.remove()
}

// or the shorthand is to use the removeAll method on the DBResultSet object
Person.query().fetch().removeAll()

其他类型的查询

除了基本的取出操作外,还有其他类型的获取操作,如countsumgroupByids

Objective-C

/* count the rows within the Person table */
int count = [[Person query] count];

/* add all of the ages together */
double total = [[Person query] sumOf:@"age"];

/* group all the people together by the surname property */
NSDictionary* peopleBySurname = [[Person query] groupBy:@"name"];

/* get just the primary keys for a query, useful to save memory */
NSArray* ids = [[Person query] ids];

Swift

/* count the rows within the Person table */
var count = Person.query().count()

/* add all of the ages together */
var total = Person.query().sumOf("age")

/* group all the people together by the surname property */
var peopleBySurname = Person.query().groupBy("name")

/* get just the primary keys for a query, useful to save memory */
var ids = Person.query().ids();

要求

  • CocoaPods 0.31