上回书说道,其实CoreData学起来也没有很复杂,我们其实增删改查都和别的ORM大同小异。但是世界总是很复杂的,一根筋的去考虑问题很容易卡到蛋,默认情况下我们的代码都在Main Thread中执行,数据库操作一旦量多了,频繁了,势必会阻塞住主线程的其他操作,俗话说,卡住了。
这个世界天然是多线程的,所以我们操作数据也必须多线程。CoreData对多线程的支持比较奇怪(按照一般的思路来说),CoreData的NSPersistentStoreCoordinator和NSManagedObjectContext对象都是不能跨线程使用的,NSManagedObject也不行,有人想加锁不就完了。No,作为一个处女座是不能忍受这么丑陋的解决方案的。其实NSManagedObjectContext已经对跨线程提供了内置的支持,只不过方式比较特殊,需要脑洞大开才行。
在创建NSManagedObject的时候有个构造器参数initWithConcurrencyType就是解决的关键所在,这个参数是一个枚举,有三个可选值:
NSConfinementConcurrencyType (或者不加参数,默认就是这个)
NSMainQueueConcurrencyType (表示只会在主线程中执行)
NSPrivateQueueConcurrencyType (表示可以在子线程中执行)
那么究竟应该怎么使用才能无阻塞无痛呢?
经过参考: http://www.cocoanetics.com/2012/07/multi-context-coredata/ 这篇文章,我们使用三层 NSManagedObjectContext 嵌套的方式。
NSManagedObjectContext是可以基于其他的 NSManagedObjectContext的,通过 setParentContext 方法,可以设置另外一个 NSManagedObjectContext 为自己的父级,这个时候子级可以访问父级下所有的对象,而且子级 NSManagedObjectContext 的内容变化后,如果执行save方法,会自动的 merge 到父级 NSManagedObjectContext 中,也就是子级save后,变动会同步到父级 NSManagedObjectContext。当然这个时候父级也必须再save一次,如果父级没有父级了,那么就会直接向NSPersistentStoreCoordinator中写入,如果有就会接着向再上一层的父级冒泡……
那么这里如同参考的文章一样,通过三个级别的 NSManagedObjectContext, 一个负责在background更新NSPersistentStoreCoordinator。一个用在主线程,主要执行插入,修改和删除操作,一些小的查询也可以在这里同步执行,如果有大的查询,就起一个新的 NSPrivateQueueConcurrencyType 类型的 NSManagedObjectContext,然后放在后台去执行查询,查询完成后将结果返回主线程。
NSManagedObjectContext在后台线程执行是通过 performBlock 方法来实现的,在传入的匿名block中执行的代码就是在子线程中了,比如
[_bgObjectContext performBlock:^{ |
__block NSError *inner_error = nil; |
[_bgObjectContext save:&inner_error]; |
那么如果是查询的话,因为 NSManagedObject 也不能跨线程访问,所以在block里获取到的NSManagedObject对象只能将objectid传到主线程,主线程再通过 objectWithID 恢复对象的方法,如下面的示例:
+(void)one:(NSString*)predicate on:(ObjectResult)handler{ |
NSManagedObjectContext *ctx = [[mmDAO instance] createPrivateObjectContext]; |
NSFetchRequest *fetchRequest = [self makeRequest:ctx predicate:predicate orderby:nil offset:0 limit:1]; |
NSArray* results = [ctx executeFetchRequest:fetchRequest error:&error]; |
NSLog(@"error: %@", error); |
[[mmDAO instance].mainObjectContext performBlock:^{ |
if ([results count]<1) { |
[[mmDAO instance].mainObjectContext performBlock:^{ |
NSManagedObjectID *objId = ((NSManagedObject*)results[0]).objectID; |
[[mmDAO instance].mainObjectContext performBlock:^{ |
handler([[mmDAO instance].mainObjectContext objectWithID:objId], nil); |
最后我们将上述的要点结合起来,解决方案就呼之欲出了。
首先我们还是需要一个抽象存储和数据操作的核心
// Created by LiMing on 14-6-24. |
// Copyright (c) 2014年 Alexander. All rights reserved. |
#import <Foundation/Foundation.h> |
typedef void(^OperationResult)(NSError* error); |
@interface mmDAO : NSObject |
@property (readonly, strong, nonatomic) NSOperationQueue *queue; |
@property (readonly ,strong, nonatomic) NSManagedObjectContext *bgObjectContext; |
@property (readonly, strong, nonatomic) NSManagedObjectContext *mainObjectContext; |
-(void) setupEnvModel:(NSString *)model DbFile:(NSString*)filename; |
- (NSManagedObjectContext *)createPrivateObjectContext; |
-(NSError*)save:(OperationResult)handler; |
// Created by LiMing on 14-6-24. |
// Copyright (c) 2014年 Alexander. All rights reserved. |
#import "mmAppDelegate.h" |
static mmDAO *onlyInstance; |
@property (nonatomic, copy)NSString *modelName; |
@property (nonatomic, copy)NSString *dbFileName; |
static dispatch_once_t onceToken; |
dispatch_once(&onceToken, ^{ |
onlyInstance = [[mmDAO alloc] init]; |
-(void) setupEnvModel:(NSString *)model DbFile:(NSString*)filename{ |
[self initCoreDataStack]; |
- (void)initCoreDataStack |
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; |
if (coordinator != nil) { |
_bgObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; |
[_bgObjectContext setPersistentStoreCoordinator:coordinator]; |
_mainObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; |
[_mainObjectContext setParentContext:_bgObjectContext]; |
- (NSManagedObjectContext *)createPrivateObjectContext |
NSManagedObjectContext *ctx = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; |
[ctx setParentContext:_mainObjectContext]; |
- (NSManagedObjectModel *)managedObjectModel |
NSManagedObjectModel *managedObjectModel; |
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:_modelName withExtension:@"momd"]; |
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; |
return managedObjectModel; |
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator |
NSPersistentStoreCoordinator *persistentStoreCoordinator = nil; |
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:_dbFileName]; |
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; |
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) { |
NSLog(@"Unresolved error %@, %@", error, [error userInfo]); |
return persistentStoreCoordinator; |
- (NSURL *)applicationDocumentsDirectory |
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; |
-(NSError*)save:(OperationResult)handler{ |
if ([_mainObjectContext hasChanges]) { |
[_mainObjectContext save:&error]; |
[_bgObjectContext performBlock:^{ |
__block NSError *inner_error = nil; |
[_bgObjectContext save:&inner_error]; |
//这里需要返回主线程,不然在回调里操作界面会出错 |
[_mainObjectContext performBlock:^{ |
然后我们扩展 NSManagedObject,把操作注入到类里,这样子有个好处是可以自动获取类名,然后就不用子级写字符串的类名了。
// NSManagedObject+helper.h |
// Created by LiMing on 14-6-24. |
// Copyright (c) 2014年 bangban. All rights reserved. |
typedef void(^ListResult)(NSArray* result, NSError *error); |
typedef void(^ObjectResult)(id result, NSError *error); |
#import <CoreData/CoreData.h> |
@interface NSManagedObject (helper) |
+(NSError*)save:(OperationResult)handler; |
+(NSArray*)filter:(NSString *)predicate orderby:(NSArray *)orders offset:(int)offset limit:(int)limit; |
+(void)filter:(NSString *)predicate orderby:(NSArray *)orders offset:(int)offset limit:(int)limit on:(ListResult)handler; |
+(id)one:(NSString*)predicate; |
+(void)one:(NSString*)predicate on:(ObjectResult)handler; |
+(void)delobject:(id)object; |
// NSManagedObject+helper.m |
// Created by LiMing on 14-6-24. |
// Copyright (c) 2014年 bangban. All rights reserved. |
#import "NSManagedObject+helper.h" |
@implementation NSManagedObject (helper) |
NSString *className = [NSString stringWithUTF8String:object_getClassName(self)]; |
return [NSEntityDescription insertNewObjectForEntityForName:className inManagedObjectContext:[mmDAO instance].mainObjectContext]; |
+(NSError*)save:(OperationResult)handler{ |
return [[mmDAO instance] save:handler]; |
+(NSArray*)filter:(NSString *)predicate orderby:(NSArray *)orders offset:(int)offset limit:(int)limit{ |
NSManagedObjectContext *ctx = [mmDAO instance].mainObjectContext; |
NSFetchRequest *fetchRequest = [self makeRequest:ctx predicate:predicate orderby:orders offset:offset limit:limit]; |
NSArray* results = [ctx executeFetchRequest:fetchRequest error:&error]; |
NSLog(@"error: %@", error); |
+(NSFetchRequest*)makeRequest:(NSManagedObjectContext*)ctx predicate:(NSString*)predicate orderby:(NSArray*)orders offset:(int)offset limit:(int)limit{ |
NSString *className = [NSString stringWithUTF8String:object_getClassName(self)]; |
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; |
[fetchRequest setEntity:[NSEntityDescription entityForName:className inManagedObjectContext:ctx]]; |
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:predicate]]; |
NSMutableArray *orderArray = [[NSMutableArray alloc] init]; |
for (NSString *order in orders) { |
NSSortDescriptor *orderDesc = nil; |
if ([[order substringToIndex:1] isEqualToString:@"-"]) { |
orderDesc = [[NSSortDescriptor alloc] initWithKey:[order substringFromIndex:1] |
orderDesc = [[NSSortDescriptor alloc] initWithKey:order |
[fetchRequest setSortDescriptors:orderArray]; |
[fetchRequest setFetchOffset:offset]; |
[fetchRequest setFetchLimit:limit]; |
+(void)filter:(NSString *)predicate orderby:(NSArray *)orders offset:(int)offset limit:(int)limit on:(ListResult)handler{ |
NSManagedObjectContext *ctx = [[mmDAO instance] createPrivateObjectContext]; |
NSFetchRequest *fetchRequest = [self makeRequest:ctx predicate:predicate orderby:orders offset:offset limit:limit]; |
NSArray* results = [ctx executeFetchRequest:fetchRequest error:&error]; |
NSLog(@"error: %@", error); |
[[mmDAO instance].mainObjectContext performBlock:^{ |
[[mmDAO instance].mainObjectContext performBlock:^{ |
NSMutableArray *result_ids = [[NSMutableArray alloc] init]; |
for (NSManagedObject *item in results) { |
NSLog(@"id=%@", item.objectID); |
[result_ids addObject:item.objectID]; |
[[mmDAO instance].mainObjectContext performBlock:^{ |
NSMutableArray *final_results = [[NSMutableArray alloc] init]; |
for (NSManagedObjectID *oid in result_ids) { |
[final_results addObject:[[mmDAO instance].mainObjectContext objectWithID:oid]]; |
handler(final_results, nil); |
+(id)one:(NSString*)predicate{ |
NSManagedObjectContext *ctx = [mmDAO instance].mainObjectContext; |
NSFetchRequest *fetchRequest = [self makeRequest:ctx predicate:predicate orderby:nil offset:0 limit:1]; |
NSArray* results = [ctx executeFetchRequest:fetchRequest error:&error]; |
if ([results count]!=1) { |
+(void)one:(NSString*)predicate on:(ObjectResult)handler{ |
NSManagedObjectContext *ctx = [[mmDAO instance] createPrivateObjectContext]; |
NSFetchRequest *fetchRequest = [self makeRequest:ctx predicate:predicate orderby:nil offset:0 limit:1]; |
NSArray* results = [ctx executeFetchRequest:fetchRequest error:&error]; |
NSLog(@"error: %@", error); |
[[mmDAO instance].mainObjectContext performBlock:^{ |
[[mmDAO instance].mainObjectContext performBlock:^{ |
NSManagedObjectID *objId = ((NSManagedObject*)results[0]).objectID; |
[[mmDAO instance].mainObjectContext performBlock:^{ |
handler([[mmDAO instance].mainObjectContext objectWithID:objId], nil); |
+(void)delobject:(id)object{ |
//[[mmDAO instance].mainObjectContext delete:object]; |
[[mmDAO instance].mainObjectContext deleteObject:object]; |
本文内容已经打包放在了github,有兴趣的可以fork下来直接用 https://github.com/ipconfiger/asyncCoreDataWrapper
另,文中代码有两处错误,已经加上了注释,提前看了的同学记得以github为准。