Permalink
Browse files

Extracting out a method to configure exactly which context is sent to…

… a block in the background.

It may be necessary in some cases to always have a fresh new context with no cached objects,
while in other cases, it's better the simply reuse the context which is in the current thread.
  • Loading branch information...
casademora committed May 27, 2012
1 parent 77aa7a7 commit a7aac60f272be4537057604343b315a931c663fb
Showing with 21 additions and 5 deletions.
  1. +5 −0 MagicalRecord/Core/MagicalRecord+Actions.h
  2. +16 −5 MagicalRecord/Core/MagicalRecord+Actions.m
@@ -20,4 +20,9 @@
+ (void) saveInBackgroundWithBlock:(void(^)(NSManagedObjectContext *localContext))block;
+ (void) saveInBackgroundWithBlock:(void(^)(NSManagedObjectContext *localContext))block completion:(void(^)(void))callback;
+/*
+ If you want to reuse the context on the current thread, use this method.
+ */
++ (void) saveInBackgroundUsingCurrentContextWithBlock:(void (^)(NSManagedObjectContext *))block completion:(void (^)(void))completion errorHandler:(void (^)(NSError *))errorHandler;
+
@end
@@ -33,18 +33,14 @@ void reset_action_queue(void)
@implementation MagicalRecord (Actions)
-+ (void) saveInBackgroundWithBlock:(void (^)(NSManagedObjectContext *))block completion:(void (^)(void))completion errorHandler:(void (^)(NSError *))errorHandler;
++ (void) saveInBackgroundUsingContext:(NSManagedObjectContext *)localContext block:(void (^)(NSManagedObjectContext *))block completion:(void(^)(void))completion errorHandler:(void(^)(NSError *))errorHandler;
{
- NSManagedObjectContext *mainContext = [NSManagedObjectContext MR_defaultContext];
- NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextWithParent:mainContext];
-
dispatch_async(action_queue(), ^{
block(localContext);
if ([localContext hasChanges])
{
[localContext MR_saveInBackgroundErrorHandler:errorHandler completion:^{
- [mainContext setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
if (completion)
{
@@ -54,6 +50,21 @@ + (void) saveInBackgroundWithBlock:(void (^)(NSManagedObjectContext *))block com
}
});
}
+
++ (void) saveInBackgroundWithBlock:(void (^)(NSManagedObjectContext *))block completion:(void (^)(void))completion errorHandler:(void (^)(NSError *))errorHandler;
+{
+ NSManagedObjectContext *mainContext = [NSManagedObjectContext MR_defaultContext];
+ NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextWithParent:mainContext];
+
+ [self saveInBackgroundUsingContext:localContext block:block completion:completion errorHandler:errorHandler];
+}
+
++ (void) saveInBackgroundUsingCurrentContextWithBlock:(void (^)(NSManagedObjectContext *))block completion:(void (^)(void))completion errorHandler:(void (^)(NSError *))errorHandler;
+{
+ NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
+
+ [self saveInBackgroundUsingContext:localContext block:block completion:completion errorHandler:errorHandler];
+}
+ (void) saveWithBlock:(void (^)(NSManagedObjectContext *localContext))block completion:(void (^)(void))completion errorHandler:(void (^)(NSError *))errorHandler;
{

0 comments on commit a7aac60

Please sign in to comment.