Added block helper method for common save operations

This commit is contained in:
Saul Mora
2010-07-28 18:54:42 -07:00
parent 228b89da4c
commit 52e88bfe68
2 changed files with 25 additions and 0 deletions

View File

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

View File

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