测试已测试 | ✓ |
语言语言 | Obj-CObjective C |
许可证 | BSD 3.0 |
发布最后发布 | 2014年12月 |
由Jesse Squires,Jesse Squires维护。
苹果的Core Data框架在业界以难以使用著称,学习曲线陡峭,且需要编写大量模板代码才能开始。此库的目标是简化Core Data堆栈的搭建,重点关注SOLID设计原则,以便您可以快速开始与您的模型进行工作。它还提供了一组实用工具,使单元测试模型层更加容易。
Core Data堆栈(通过objc.io)
# For latest release in cocoapods
pod 'RSTCoreDataKit'
# Feeling adventurous? Get the latest on develop
pod 'RSTCoreDataKit', :git => 'https://github.com/rosettastone/RSTCoreDataKit.git', :branch => 'develop'
// New school
@import RSTCoreDataKit;
// Old school
#import <RSTCoreDataKit/RSTCoreDataKit.h>
// Initialize the Core Data model, this class encapsulates the notion of a .xcdatamodeld file
// The name passed here should be the name of an .xcdatamodeld file
RSTCoreDataModel *model = [[RSTCoreDataModel alloc] initWithName:@"MyModelName"];
// Initialize the stack
RSTCoreDataStack *stack = [[RSTCoreDataStack alloc] initWithStoreURL:model.storeURL
modelURL:model.modelURL
options:nil
concurrencyType:NSMainQueueConcurrencyType];
// That's all!
或者,您可以使用便利初始化器来处理常见用例。
// Same as above, with some default options
RSTCoreDataStack *defaultStack = [RSTCoreDataStack defaultStackWithStoreURL:model.storeURL modelURL:model.modelURL];
// Create a private queue stack
RSTCoreDataStack *privateStack = [RSTCoreDataStack privateStackWithStoreURL:model.storeURL modelURL:model.modelURL];
// Create an in-memory stack
RSTCoreDataStack *inMemoryStack = [RSTCoreDataStack stackWithInMemoryStoreWithModelURL:model.modelURL];
NSManagedObjectContext *context = /* an initialized parent or child context */;
BOOL success = [RSTCoreDataContextSaver saveAndWait:context];
RSTCoreDataModel *model = [[RSTCoreDataModel alloc] initWithName:@"MyModelName"];
[model removeExistingModelStore];
RSTCoreDataModel *model = [[RSTCoreDataModel alloc] initWithName:@"MyModelName"];
BOOL needsMigration = [model modelStoreNeedsMigration];
RSTCoreDataStack *stack = /* an initialized stack */;
// Create a child context on a private queue
NSManagedObjectContext *privateChildContext = [stack newDefaultPrivateChildContext];
// Listen for saves from the child context
RSTCoreDataContextDidSaveListener *listener = [[RSTCoreDataContextDidSaveListener alloc] initWithHandler:^(NSNotification *notification) {
// child context was saved
// handle here, merge with parent context, etc.
} forManagedObjectContext:privateChildContext];
RSTCoreDataKit
包含一套单元测试。您可以在Xcode中像往常一样运行它们。
这些测试都有详细的注释,并作为如何使用此库的进一步文档。
此外,您应该对您的Core Data模型层进行单元测试。此测试套件也是如何进行此操作的示例。
// Create an in-memory store for testing purposes
// You can create this before each test, and tear it down after
RSTCoreDataModel *model = [[RSTCoreDataModel alloc] initWithName:@"MyModelName"];
RSTCoreDataStack *inMemoryStackForTesting = [RSTCoreDataStack stackWithInMemoryStoreWithModelURL:model.modelURL];
// Alernatively, you could persist a store to disk for your tests
RSTCoreDataStack *defaultStackForTesting = [RSTCoreDataStack defaultStackWithStoreURL:model.storeURL modelURL:model.modelURL];
// Then remove the store as needed
[model removeExistingModelStore];
优秀的文档可通过此处查看,由@CocoaDocs提供。
其他有用的资源
参见CONTRIBUTING.md
。
RSTCoreDataKit
基于BSD 3.0许可证发布。详情请见LICENSE
。
版权所有 © 2014 Rosetta Stone。