ActiveRecord 1.2.2

ActiveRecord 1.2.2

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最后发布2014年12月

Michal Konturek 维护。



 
依赖
MKFoundationKit/Block>= 1.0.0
MKFoundationKit/NSNumber>= 1.0.0
RubySugar>= 1.1.0
 

一个轻量级的Active Record实现,用于Core Data。

许可证

此项目的源代码可在标准MIT许可证下获得。请参阅许可证文件

用法

只需导入#import "ActiveRecord.h"(请参阅示例

// create an entity
Student *student = [Student create];
student.firstName = @"Adam";
student.lastName = @"Smith";
student.age = @21;

// commit changes in context
[Student commit];

// rollback context to last commit
[student delete];
[Student rollback];

// fetch all Student Objects
[Student objects];

// queries
[Student objects:@"age > 20"];
[Student objects:@"lastName == 'Smith'"];
[Student objects:@{@"age": @21, @"lastName": @"Smith"}];

// ordered queries
[Student ordered:@"lastName, age"]; // orders by name ASC, age ASC
[Student ordered:@"lastName, !age"]; // orders by name ASC, age DESC

[Student objects:@"age > 20" ordered:@"!age"];

// Erase all Student objects
[Student deleteAll];

JSON序列化

Active Record允许您直接从JSON对象(即形式为NSDictionary的JSON字符串)创建NSManagedObject实例。只要JSON对象中属性和关系名称与您模型中的名称匹配,Active Record就会为您做大部分的重体力劳动。

例如,传递以下JSON内容的文件

{
    "uid": 0,
    "age": 32,
    "firstName": "Jaclyn",
    "lastName": "Petty",
    "course": {
        "uid": 1,
        "name": "Software Engineering"
    },
    "modules": [
        {
            "uid": 0,
            "name": "Module 0"
        },
        {
            "uid": 1,
            "name": "Module 1"
        }
    ],
    "registration": {
        "uid": 0,
        "signature": "8a700e2b-f14c-4b69-bcd6-f8baddabb1da"
    }
}

将导致创建与一个Course相关联的Student对象、一个Registration对象和两个Module对象。

id path = [[NSBundle mainBundle] URLForResource:@"Example" withExtension:@"json"];
id json = [NSData dataWithContentsOfURL:path];
id data = [NSJSONSerialization JSONObjectWithData:json options:kNilOptions error:nil];

id student = [Student createOrUpdateWithData:data];

有关更详细的信息,请参阅与此项目相关的测试。

后台处理

使用Active Record的后台处理很简单。只需在后台线程上执行您的代码,Active Record就会处理剩余部分。它将自动创建具有NSConfinementConcurrencyType的后台上下文,并将其父上下文设置为您的主(前台)上下文。

此方法保证了每次你在子(后台)上下文中保存(提交)更改时,它们都会被推送到你的父(主)上下文。

请参阅示例

id students = [Factory fixture1000];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    [students mk_each:^(id item) {
        [[Student createOrUpdateWithData:item] commit];
    }];

    dispatch_async(dispatch_get_main_queue(), ^{
        // notify about completion
    });
});

API

NSManagedObject+AR

+ (instancetype)createWithAutoID;
+ (instancetype)createWithID:(NSNumber *)objectID;
+ (instancetype)create;

+ (NSEntityDescription *)entityDescription;
+ (NSString *)entityName;
+ (NSString *)primaryKey;

+ (void)deleteAll;

- (void)assignAutoID;
- (void)delete;

NSManagedObject+AR_Finders

+ (BOOL)hasObjects;
+ (BOOL)hasObjects:(id)condition;
+ (BOOL)hasObjectsWithPredicate:(NSPredicate *)predicate;

+ (NSInteger)count;
+ (NSInteger)count:(id)condition;
+ (NSInteger)countWithPredicate:(NSPredicate *)predicate;

+ (instancetype)object:(id)condition;
+ (instancetype)objectWithID:(NSNumber *)objectID;
+ (instancetype)objectWithPredicate:(NSPredicate *)predicate;
+ (instancetype)objectWithMaxValueFor:(NSString *)attribute;
+ (instancetype)objectWithMinValueFor:(NSString *)attribute;

+ (NSArray *)orderedAscendingBy:(NSString *)key;
+ (NSArray *)orderedDescendingBy:(NSString *)key;
+ (NSArray *)ordered:(id)order;

+ (NSArray *)objects;
+ (NSArray *)objects:(id)condition;
+ (NSArray *)objects:(id)condition ordered:(id)order;
+ (NSArray *)objectsWithPredicate:(NSPredicate *)predicate;
+ (NSArray *)objectsWithPredicate:(NSPredicate *)predicate
               withSortDescriptor:(NSSortDescriptor *)descriptor;
+ (NSArray *)objectsWithPredicate:(NSPredicate *)predicate
              withSortDescriptors:(NSArray *)descriptors;

NSPredicate+AR

+ (instancetype)and:(NSArray *)conditions;
+ (instancetype)or:(NSArray *)conditions;

+ (instancetype)and:(id)cond1 :(id)cond2;
+ (instancetype)or:(id)cond1 :(id)cond2;

+ (instancetype)createFrom:(id)object;

- (instancetype)and:(id)condition;
- (instancetype)or:(id)condition;

NSSortDescriptor+AR

+ (NSArray *)descriptors:(id)object;
+ (instancetype)create:(id)object;
+ (instancetype)createWithKey:(NSString *)key ascending:(BOOL)ascending;

NSManagedObjectContext+AR

+ (instancetype)managedObjectContext;
+ (instancetype)mainContext;
+ (instancetype)backgroundContext;
+ (void)removeBackgroundContext;
+ (void)setBackgroundContext:(NSManagedObjectContext *)context;

+ (NSFetchRequest *)requestWithPredicate:(NSPredicate *)predicate
                      withSortDescriptor:(NSSortDescriptor *)descriptor;

+ (NSFetchRequest *)requestWithPredicate:(NSPredicate *)predicate
                     withSortDescriptors:(NSArray *)descriptors;

+ (instancetype)createWithPredicate:(NSPredicate *)predicate
                 withSortDescriptor:(NSSortDescriptor *)descriptor;
+ (instancetype)createWithPredicate:(NSPredicate *)predicate
                withSortDescriptors:(NSArray *)descriptors;