RSTCoreDataKit 0.1.2

RSTCoreDataKit 0.1.2

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

Jesse SquiresJesse Squires维护。



  • 作者:
  • Rosetta Stone

关于

苹果的Core Data框架在业界以难以使用著称,学习曲线陡峭,且需要编写大量模板代码才能开始。此库的目标是简化Core Data堆栈的搭建,重点关注SOLID设计原则,以便您可以快速开始与您的模型进行工作。它还提供了一组实用工具,使单元测试模型层更加容易。

Core Data堆栈(通过objc.io

stack img

要求

  • iOS 8.0+
  • ARC
  • Xcode 6+

安装

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

搭建您的Core Data堆栈

// 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。