mirror of
https://github.com/zhigang1992/MagicalRecord.git
synced 2026-01-12 22:48:38 +08:00
Added block helper method for common save operations
This commit is contained in:
@@ -8,6 +8,12 @@
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#ifdef NS_BLOCKS_AVAILABLE
|
||||
|
||||
@class NSManagedObjectContext;
|
||||
typedef void (^CoreDataBlock)(NSManagedObjectContext *);
|
||||
|
||||
#endif
|
||||
|
||||
@interface ActiveRecordHelpers : NSObject {
|
||||
|
||||
@@ -25,4 +31,10 @@
|
||||
+ (void) setupDefaultCoreDataStackWithStoreNamed:(NSString *)storeName;
|
||||
+ (void) setupCoreDataStackWithAutoMigratingSqliteStoreNamed:(NSString *)storeName;
|
||||
|
||||
#ifdef NS_BLOCKS_AVAILABLE
|
||||
|
||||
+ (void) performSaveDataOperationWithBlock:(CoreDataBlock)block;
|
||||
|
||||
#endif
|
||||
|
||||
@end
|
||||
|
||||
@@ -96,4 +96,17 @@
|
||||
[NSManagedObjectContext setDefaultContext:context];
|
||||
}
|
||||
|
||||
#ifdef NS_BLOCKS_AVAILABLE
|
||||
+ (void) performSaveDataOperationWithBlock:(CoreDataBlock)block
|
||||
{
|
||||
NSManagedObjectContext *localContext = [NSManagedObjectContext contextThatNotifiesDefaultContextOnMainThread];
|
||||
[localContext setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
|
||||
|
||||
block(localContext);
|
||||
|
||||
[localContext save];
|
||||
[[NSManagedObjectContext defaultContext] stopObservingContext:localContext];
|
||||
}
|
||||
#endif
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user