diff --git a/ActiveRecordHelpers.h b/ActiveRecordHelpers.h index c202d43..3981ea1 100644 --- a/ActiveRecordHelpers.h +++ b/ActiveRecordHelpers.h @@ -8,6 +8,12 @@ #import +#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 diff --git a/ActiveRecordHelpers.m b/ActiveRecordHelpers.m index 092c13a..7e5c86c 100644 --- a/ActiveRecordHelpers.m +++ b/ActiveRecordHelpers.m @@ -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