上回書說道,其實CoreData學起來也沒有很複雜,咱們其實增刪改查都和別的ORM大同小異。可是世界老是很複雜的,一根筋的去考慮問題很容易卡到蛋,默認狀況下咱們的代碼都在Main Thread中執行,數據庫操做一旦量多了,頻繁了,勢必會阻塞住主線程的其餘操做,俗話說,卡住了。github
這個世界自然是多線程的,因此咱們操做數據也必須多線程。CoreData對多線程的支持比較奇怪(按照通常的思路來講),CoreData的NSPersistentStoreCoordinator和NSManagedObjectContext對象都是不能跨線程使用的,NSManagedObject也不行,有人想加鎖不就完了。No,做爲一個處女座是不能忍受這麼醜陋的解決方案的。其實NSManagedObjectContext已經對跨線程提供了內置的支持,只不過方式比較特殊,須要腦洞大開才行。數據庫
在建立NSManagedObject的時候有個構造器參數initWithConcurrencyType就是解決的關鍵所在,這個參數是一個枚舉,有三個可選值:多線程
NSConfinementConcurrencyType (或者不加參數,默認就是這個)
NSMainQueueConcurrencyType (表示只會在主線程中執行)
NSPrivateQueueConcurrencyType (表示能夠在子線程中執行)
那麼究竟應該怎麼使用才能無阻塞無痛呢?app
通過參考: http://www.cocoanetics.com/2012/07/multi-context-coredata/ 這篇文章,咱們使用三層 NSManagedObjectContext 嵌套的方式。async
NSManagedObjectContext是能夠基於其餘的 NSManagedObjectContext的,經過 setParentContext 方法,能夠設置另一個 NSManagedObjectContext 爲本身的父級,這個時候子級能夠訪問父級下全部的對象,並且子級 NSManagedObjectContext 的內容變化後,若是執行save方法,會自動的 merge 到父級 NSManagedObjectContext 中,也就是子級save後,變更會同步到父級 NSManagedObjectContext。固然這個時候父級也必須再save一次,若是父級沒有父級了,那麼就會直接向NSPersistentStoreCoordinator中寫入,若是有就會接着向再上一層的父級冒泡……post
那麼這裏如同參考的文章同樣,經過三個級別的 NSManagedObjectContext, 一個負責在background更新NSPersistentStoreCoordinator。一個用在主線程,主要執行插入,修改和刪除操做,一些小的查詢也能夠在這裏同步執行,若是有大的查詢,就起一個新的 NSPrivateQueueConcurrencyType 類型的 NSManagedObjectContext,而後放在後臺去執行查詢,查詢完成後將結果返回主線程。fetch
NSManagedObjectContext在後臺線程執行是經過 performBlock 方法來實現的,在傳入的匿名block中執行的代碼就是在子線程中了,好比atom
[_bgObjectContext performBlock:^{ |
__block NSError *inner_error = nil; |
[_bgObjectContext save:&inner_error]; |
那麼若是是查詢的話,由於 NSManagedObject 也不能跨線程訪問,因此在block裏獲取到的NSManagedObject對象只能將objectid傳到主線程,主線程再經過 objectWithID 恢復對象的方法,以下面的示例:spa
+(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爲準。