当前位置:天才代写 > tutorial > 其他教程 > HardCoreData:UI线程非阻塞CoreData栈和控制器

HardCoreData:UI线程非阻塞CoreData栈和控制器

2018-05-16 08:00 星期三 所属: 其他教程 浏览:356

本资源由 伯乐在线 – 张发白 整理

hardcoredata

HardCoreData是基于Marcus Zarra 的多线程方法创建的 Core Data 栈,能够将写入到私有队列解耦,保持UI操作的流畅性。

image1

HardCoreData由两个基本元素组成: HCDCoreDataStack 和 HCDCoreDataStackController。

HCDCoreDataStack

封装本地 CoreData 栈设置

Objective-C

1234567891011121314151617 /* Convenience initializers */ + (instancetype)binaryStackWithName:(NSString *)modelName;+ (instancetype)inMemoryStackWithName:(NSString *)modelName;+ (instancetype)sqliteStackWithName:(NSString *)modelName;+ (instancetype)stackWithModelName:(NSString *)modelName storeType:(NSString *)storeType; /** *  Designated initializer * *  @param modelName NSString *  @param storeType NSString * *  @return HCDCoreDataStack */ – (instancetype)initWithModelName:(NSString *)modelName storeType:(NSString *)storeType NS_DESIGNATED_INITIALIZER;

HCDCoreDataStack protocol

因为 HCDCoreDataStack 不仅是一个类,还是一个协议,所以你可以通过实现协议来创建自定义的栈。

Objective-C

1234567891011121314 @protocol HCDCoreDataStack <NSObject> @required /* Represents current store coordinator */@property (nonatomic, strong, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator; /* Represents currentm object model */@property (nonatomic, strong, readonly) NSManagedObjectModel *managedObjectModel; /* Represents your single source of truth. Associated with main queue. */@property (nonatomic, strong, readonly) NSManagedObjectContext *mainManagedObjectContext; @end

HCDCoreDataStackController

创建子上下文与保存操作。

Objective-C

1234567891011121314151617181920 /** *  Designated initializer * *  @param stack HCDCoreDataStack * *  @return HCDCoreDataStackController */- (instancetype)initWithStack:(id <HCDCoreDataStack>)stack NS_DESIGNATED_INITIALIZER; /** * Instantiates new child context with given concurrency type.(使用给定的并发类型实例化新的子上下文) * You are responsive for retaining. */- (NSManagedObjectContext *)createChildContextWithType:(NSManagedObjectContextConcurrencyType)type; /** *  保存主上下文(main context),并将更改(changes)推送到持久化保存。 *  执行时会保证线程安全 */- (void)save;

应用实例

#p#分页标题#e#

Objective-C

12345678910111213141516171819202122 /* Create core data stack */HCDCoreDataStack *stack = [HCDCoreDataStack sqliteStackWithName:@”Model”]; HCDCoreDataStackController *coreDataController = [HCDCoreDataStackController controllerWithStack:stack]; NSManagedObjectContext *backgroundContext = [coreDataController createChildContextWithType:NSPrivateQueueConcurrencyType];   [backgroundContext performBlock:^{     Person *person = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([Person class]) inManagedObjectContext:backgroundContext];     person.firstName = @”John”;     person.lastName = @”Doe”;     /* 保存子上下文 */       [backgroundContext save:nil];     /* 将数据保存到持久化存储 */       [coreDataController save]; }];

使用

如果你想要运行示例工程,将仓库克隆到本地,然后在示例项目路径下运行 pod install 。

安装

可以通过CocoaPods安装 HardCoreData,只需在podfile中添加以下内容即可安装:

Shell

1 pod “HardCoreData”

作者

Serg Krivoblotsky, krivoblotsky@me.com

历史版本

发布版本

开源地址:http://github.com/Krivoblotsky/HardCoreData

 

    关键字:

天才代写-代写联系方式