Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'private_queue_constant' of github.com:fossli/MagicalRec…

…ord into private_queue_constant
  • Loading branch information...
commit 4216a83dde60d2280628a41f51dd15aa260f81e9 2 parents 9d95747 + 8123d3a
Håvard Fossli authored March 27, 2012
4  Source/Categories/NSManagedObjectContext+MagicalRecord.h
@@ -32,6 +32,10 @@ extern NSString * const kMagicalRecordDidMergeChangesFromiCloudNotification;
32 32
 + (void) MR_setDefaultContext:(NSManagedObjectContext *)moc;
33 33
 + (void) MR_resetContextForCurrentThread;
34 34
 
  35
+#if TEST
  36
++ (void)resetContextMap;
  37
+#endif
  38
+
35 39
 + (NSManagedObjectContext *) MR_context;
36 40
 + (NSManagedObjectContext *) MR_contextForCurrentThread;
37 41
 
30  Source/Categories/NSManagedObjectContext+MagicalRecord.m
@@ -13,6 +13,11 @@
13 13
 static void const * kMagicalRecordNotifiesMainContextAssociatedValueKey = @"kMagicalRecordNotifiesMainContextOnSave";
14 14
        NSString * const kMagicalRecordDidMergeChangesFromiCloudNotification = @"kMagicalRecordDidMergeChangesFromiCloudNotification";
15 15
 
  16
+#if TEST
  17
+static NSMutableDictionary *contextMap;
  18
+#endif
  19
+
  20
+
16 21
 @interface NSManagedObjectContext (MagicalRecordPrivate)
17 22
 
18 23
 - (void) MR_mergeChangesFromNotification:(NSNotification *)notification;
@@ -276,7 +281,21 @@ + (NSManagedObjectContext *) MR_contextForCurrentThread;
276 281
 	}
277 282
 	else
278 283
 	{
279  
-        NSManagedObjectContext *context = [self MR_contextThatNotifiesDefaultContextOnMainThread];
  284
+        static dispatch_once_t onceToken;
  285
+        dispatch_once(&onceToken, ^{
  286
+            contextMap = [[NSMutableDictionary alloc] init];
  287
+        });
  288
+        
  289
+        if([[NSThread currentThread] name] == nil || [[[NSThread currentThread] name] isEqualToString:@""]){
  290
+            [[NSThread currentThread] setName:[NSString stringWithFormat:@"%i", [[NSDate date] timeIntervalSince1970]]];
  291
+        }
  292
+        
  293
+        NSManagedObjectContext *context = [contextMap objectForKey:[[NSThread currentThread] name]];
  294
+        
  295
+        if(context == nil){
  296
+            context = [self MR_contextThatNotifiesDefaultContextOnMainThread];
  297
+            [contextMap setObject:context forKey:[[NSThread currentThread] name]];
  298
+        }
280 299
 		return context;
281 300
 	}
282 301
     
@@ -303,6 +322,15 @@ + (NSManagedObjectContext *) MR_contextForCurrentThread;
303 322
 
304 323
 }
305 324
 
  325
+#if TEST
  326
++ (void)resetContextMap{
  327
+    
  328
+    [contextMap release];
  329
+    contextMap = [[NSMutableDictionary alloc] init];
  330
+
  331
+}
  332
+#endif
  333
+
306 334
 + (NSManagedObjectContext *) MR_contextWithStoreCoordinator:(NSPersistentStoreCoordinator *)coordinator;
307 335
 {
308 336
 	NSManagedObjectContext *context = nil;

0 notes on commit 4216a83

Please sign in to comment.
Something went wrong with that request. Please try again.